Maven plugin usage

The Resources plugin is responsible for processing project resource files and copying them to the output directory. Maven separates main resources and test resources. Generally, main resources are associated with main source code, while test resources are associated with test source code.

The Resources plugin has three goals:

  1. resources:resources, copy main resources to main output directory. It binds the process-resources lifecycle phase, which is executed before the Compiler:compile plugin goal is executed.
  2. resources: testResources, copy test resources to test output directory. It binds the process-test-resources lifecycle phase, which is executed before the surefire:test plugin goal is executed.
  3. resources:copy-resources, manually copy resources to the output directory

You can specify the character encoding that the resources plugin reads and writes files in, such as ASCII, UTF-8 or UTF-16. The ${project.build.sourceEncoding} property can also be specified.

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

The encoding can also be specified via <configuration>

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

By default, Maven will look for resources in the project's src/main/resources directory. If your resources are not in this directory, you can specify them with the <resources> tag, and multiple directories are also supported.

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

Sometimes, there are variable references in the resource file, you can use the <filtering> tag to specify whether to replace the variables in the resource. The source of the variable is the variable defined in the <properties> tag in the pom file. Filter resources can also be defined in <build>.

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

It is possible that all resource files in the directory need to be used, which can be finely controlled using <includes> and <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> 

If the ${} character originally exists in the resource and does not need to be replaced, you can add \ before $ and use <escapeString> in <configuration>.

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

In addition, there are binary files in the directory, which need to be excluded. You can also use <nonFilteredFileExtensions> in <configuration> to filter according to the suffix.

<!-- 过滤后缀为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> 

If you need to copy resource files at other stages, you can use the plugin target 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

When the project depends on other jar packages, the executed jar package fails and this exception is thrown.

Reason: Because there are redundant .SF files in META-INF in the dependent jar package that conflict with the current jar package,

  • Solution one

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

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

Reprinted in: https://www.cnblogs.com/Vowzhou/p/11051502.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324132336&siteId=291194637