【问题标题】:Maven default Resources not copying resourcesMaven默认资源不复制资源
【发布时间】:2018-08-24 10:47:27
【问题描述】:

这是我的项目目录结构:

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

资源按预期复制。

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

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

如果我明确配置资源插件,为什么它会起作用,如果我不这样做,为什么它不起作用?

【问题讨论】:

    标签: java eclipse maven m2eclipse maven-resources-plugin


    【解决方案1】:

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

    编辑: 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>
    

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

    【讨论】:

      【解决方案2】:

      如果您想要target/dist/resources 中的文件如输出所示,您必须在插件中配置输出目录。默认输出目录为target/classes

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-16
        • 1970-01-01
        • 1970-01-01
        • 2020-11-18
        • 1970-01-01
        • 1970-01-01
        • 2013-05-28
        相关资源
        最近更新 更多