Maven 默认资源不复制资源
Maven default Resources not copying resources
这是我的项目目录结构:
这是我的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>borsa</groupId>
<artifactId>borsa</artifactId>
<version>1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<finalName>aggiornamento</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/dist/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>${basedir}/target/dist</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>
borsa.Application
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>ethereal</groupId>
<artifactId>ethereal</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>common-parser</groupId>
<artifactId>common-parser</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>common-util</groupId>
<artifactId>common-util</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
</dependencies>
</project>
如果我运行maven包或maven安装资源没有复制到目标文件夹,即使在控制台出现输出:
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ borsa ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
但是,如果我在 pom 中添加资源插件的手动配置:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>process-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dist/resources</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>*</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
资源按预期复制。
所以问题是:目标资源运行的资源插件不应该自动吗?
不应该自动扫描scr/main/resources文件夹中的资源和复制到目标文件夹的资源吗?
如果我明确配置资源插件,为什么它可以工作,如果我不配置它为什么不工作?
如果您想要target/dist/resources
中的文件如输出所示,您必须在插件中配置输出目录。默认输出目录是target/classes
.
找到答案了。 maven-resources-plugin 的默认行为是将资源 打包到 jar 中。如果您使用 zip 程序打开 jar,您将看到其中的资源。要使用默认目标(资源)更改默认阶段(process-resources)中默认资源插件(maven-resources-plugin)的行为,您必须使用标签 <resources>
作为 [=13 的子标签=]:
编辑: maven-resources-plugin 的默认行为是将资源打包到 jar 以及 target/classes文件夹.
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}/dist/resources</targetPath>
</resource>
</resources>
这样资源就会在指定文件夹中的 jar 之外。如果你想发送一些资源在罐子里,一些在外面:
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}/dist/resources</targetPath>
<includes>
<include>this_file_will_go_outside_the_ jar_in_the_folder.txt</include>
<include>this_too.txt</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<!-- without targetPath everything not excluded will be packaged inside the jar -->
<excludes>
<exclude>this_file_will_go_outside_the_ jar_in_the_folder.txt</exclude>
<exclude>this_too.txt</exclude>
</excludes>
</resource>
</resources>
编辑: 请注意,第一个选项资源仍将被复制到 target/classes 文件夹(和 jar/artifact 中)。排除的资源将不会出现在类路径中,因此您将无法通过 运行 Eclipse 中的代码访问它们。您将只能从清单文件中将 targetPath 文件夹指定为类路径的可执行 jar 访问它们。更好的选择是同时使用 mave-jar-plugin 和 maven-resources-plugin。请参阅 and 答案。
这是我的项目目录结构:
这是我的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>borsa</groupId>
<artifactId>borsa</artifactId>
<version>1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<finalName>aggiornamento</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/dist/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>${basedir}/target/dist</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>
borsa.Application
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>ethereal</groupId>
<artifactId>ethereal</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>common-parser</groupId>
<artifactId>common-parser</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>common-util</groupId>
<artifactId>common-util</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
</dependencies>
</project>
如果我运行maven包或maven安装资源没有复制到目标文件夹,即使在控制台出现输出:
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ borsa ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
但是,如果我在 pom 中添加资源插件的手动配置:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>process-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dist/resources</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>*</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
资源按预期复制。
所以问题是:目标资源运行的资源插件不应该自动吗?
不应该自动扫描scr/main/resources文件夹中的资源和复制到目标文件夹的资源吗?
如果我明确配置资源插件,为什么它可以工作,如果我不配置它为什么不工作?
如果您想要target/dist/resources
中的文件如输出所示,您必须在插件中配置输出目录。默认输出目录是target/classes
.
找到答案了。 maven-resources-plugin 的默认行为是将资源 打包到 jar 中。如果您使用 zip 程序打开 jar,您将看到其中的资源。要使用默认目标(资源)更改默认阶段(process-resources)中默认资源插件(maven-resources-plugin)的行为,您必须使用标签 <resources>
作为 [=13 的子标签=]:
编辑: maven-resources-plugin 的默认行为是将资源打包到 jar 以及 target/classes文件夹.
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}/dist/resources</targetPath>
</resource>
</resources>
这样资源就会在指定文件夹中的 jar 之外。如果你想发送一些资源在罐子里,一些在外面:
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}/dist/resources</targetPath>
<includes>
<include>this_file_will_go_outside_the_ jar_in_the_folder.txt</include>
<include>this_too.txt</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<!-- without targetPath everything not excluded will be packaged inside the jar -->
<excludes>
<exclude>this_file_will_go_outside_the_ jar_in_the_folder.txt</exclude>
<exclude>this_too.txt</exclude>
</excludes>
</resource>
</resources>
编辑: 请注意,第一个选项资源仍将被复制到 target/classes 文件夹(和 jar/artifact 中)。排除的资源将不会出现在类路径中,因此您将无法通过 运行 Eclipse 中的代码访问它们。您将只能从清单文件中将 targetPath 文件夹指定为类路径的可执行 jar 访问它们。更好的选择是同时使用 mave-jar-plugin 和 maven-resources-plugin。请参阅