Maven插件使用

mac2022-06-30  70

Resources插件负责处理项目资源文件并拷贝到输出目录。Maven将main resources和test resources分开,一般main resources关联main source code,而test resources关联test source code。

Resources插件目标有三个:

resources:resources,拷贝main resources到main output directory。它绑定了process-resources生命周期阶段,当执行Compiler:compile插件目标前就会执行此阶段。resources:testResources,拷贝test resources到test output directory。它绑定了process-test-resources生命周期阶段,当执行surefire:test插件目标前就会执行此阶段。resources:copy-resources,手动拷贝资源到输出目录

可以指定resources插件读取和写入文件的字符编码,比如ASCII,UTF-8或UTF-16。也可以指定${project.build.sourceEncoding}属性。

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>

也可以通过<configuration>指定编码

<build> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>

默认情况下,Maven会从项目的src/main/resources目录下查找资源。如果你的资源不在此目录下,可以用<resources>标签指定,同时也支持多个目录。

<build> <resources> <resource> <directory>src/main/resources1</directory> </resource> <resource> <directory>src/main/resources2</directory> </resource> </resources> </build>

有的时候,资源文件中存在变量引用,可以使用<filtering>标签指定是否替换资源中的变量。变量的来源为pom文件中的<properties>标签中定义的变量。也可以在<build>中定义过滤器资源。

<build> <filters> <filter>filter-values.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>

可能目录下的资源文件都需要被使用,可以使用<includes>和<excludes>来精细控制。

<build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.txt</include> <include>**/*.rtf</include> </includes> <excludes> <exclude>**/*.bmp</exclude> <exclude>**/*.jpg</exclude> <exclude>**/*.jpeg</exclude> <exclude>**/*.gif</exclude> </excludes> </resource> <resources> </build>

如果资源中本来存在${}字符,不需要被替换,可以在$前加\,并在<configuration>中使用<escapeString>。

<plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <configuration> <escapeString>\</escapeString> <encoding>UTF-8</encoding> </configuration> </plugin> <plugins>

另外目录下存在二进制文件,需要排除,也可以在<configuration>中使用<nonFilteredFileExtensions>根据后缀来过滤。

<!-- 过滤后缀为pdf和swf的文件 --> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <configuration> <encoding>UTF-8</encoding> <nonFilteredFileExtensions> <nonFilteredFileExtension>pdf</nonFilteredFileExtension> <nonFilteredFileExtension>swf</nonFilteredFileExtension> </nonFilteredFileExtensions> </configuration> </plugin> <plugins>

如果你需要在其他阶段拷贝资源文件,可以使用插件目标copy-resources。

<build> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <executions> <execution> <id>copy-resources</id> <!-- here the phase you need --> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/extra-resources</outputDirectory> <resources> <resource> <directory>src/non-packaged-resources</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build>

解决java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

当项目依赖其他jar包的时候,打出的jar包执行出错,抛出这个异常。

原因:因为依赖jar包中的META-INF中有多余的.SF文件与当前jar包冲突,

解决方案 一

在打包前删除依赖jar包的.SF文件

解决方案 二

在打完的jar包执行 zip -d your.jar 'META-INF/.SF' 'META-INF/.RSA' 'META-INF/*SF'  

转载于:https://www.cnblogs.com/Vowzhou/p/11051502.html

最新回复(0)