Maven default Resources not copying resources
这是我的项目目录结构:
这是我的pom.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | <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> 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> 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> 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> maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <outputDirectory>${basedir}/target/dist</outputDirectory> <manifest> true</addClasspath> <classpathPrefix>libs/</classpathPrefix> <mainClass> borsa.Application </mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> maven-surefire-plugin</artifactId> <version>2.20.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.poi</groupId> poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.jsoup</groupId> jsoup</artifactId> <version>1.11.2</version> </dependency> <dependency> <groupId>junit</groupId> junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> poi-ooxml</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.springframework</groupId> spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>ethereal</groupId> ethereal</artifactId> <version>1</version> </dependency> <dependency> <groupId>common-parser</groupId> common-parser</artifactId> <version>1</version> </dependency> <dependency> <groupId>common-util</groupId> common-util</artifactId> <version>1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> log4j-api</artifactId> <version>2.10.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> log4j-core</artifactId> <version>2.10.0</version> </dependency> </dependencies> </project> |
如果我运行maven软件包或maven安装资源没有复制到目标文件夹,即使在控制台中出现输出,也是如此:
1 2 3 | [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ borsa --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 2 resources |
但是,如果我在pom中添加了资源插件的手动配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <plugin> 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文件夹中的资源以及复制到目标文件夹中的资源?
如果我显式配置资源插件,为什么它起作用,如果我不配置它,为什么它不起作用?
找到了答案。 maven-resources-plugin的默认行为是将资源打包在jar中。如果使用zip程序打开jar,则会看到其中的资源。要在默认阶段(流程资源)和默认目标(资源)更改默认资源插件(maven-resources-plugin)的行为,必须将标签
编辑:maven-resources-plugin的默认行为是将jar中的资源打包在target / classes文件夹旁边。
1 2 3 4 5 6 | <resources> <resource> <directory>src/main/resources</directory> <targetPath>${project.build.directory}/dist/resources</targetPath> </resource> </resources> |
这样,资源将移出指定文件夹中的jar。如果要在jar中发送一些资源,而在外部发送一些资源:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <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运行代码来访问它们。您将只能通过清单文件中指定为classpath的targetPath文件夹的可执行jar访问它们。更好的选择是配置mave-jar-plugin和maven-resources-plugin。请参阅此答案。
如果要在输出中显示