gpt4 book ai didi

java - Maven 默认资源不复制资源

转载 作者:行者123 更新时间:2023-11-29 08:29:44 25 4
gpt4 key购买 nike

这是我的项目目录结构:

enter image description here

这是我的 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>

资源按预期复制。

enter image description here

所以问题是:目标资源的resources-plugin不应该自动运行吗?

难道不应该自动扫描 scr/main/resources 文件夹中的资源和复制到目标文件夹的资源吗?

为什么我显式配置 resources-plugin 会起作用,如果我不显式配置它为什么不起作用?

最佳答案

找到了答案。 maven-resources-plugin 的默认行为是将资源打包到jar 中。如果您使用 zip 程序打开 jar,您将看到其中的资源。要更改默认资源插件 (maven-resources-plugin) 在默认阶段 (process-resources) 和默认目标 (resources) 的行为,您必须使用标签 <resources>作为 <build> 的 child :

编辑:maven-resources-plugin 的默认行为是将资源打包到 jar 中在 target/classes 文件夹中 .

<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}/dist/resources</targetPath>
</resource>
</resources>

这样,资源将在指定文件夹中的 jar 之外。如果你想发送一些资源在 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>

编辑:请注意,第一个选项的资源仍将被复制到目标/类文件夹(以及 jar/Artifact 中)。排除的资源不会出现在类路径中,因此您将无法通过从 Eclipse 运行代码来访问它们。您将只能从可执行 jar 访问它们,并将 targetPath 文件夹指定为 list 文件中的类路径。更好的选择是配置 mave-jar-plugin 和 maven-resources-plugin。参见 thisthis回答。

关于java - Maven 默认资源不复制资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49302596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com