Maven默认资源不复制资源

时间:2018-03-15 14:50:37

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

这是我的项目目录结构:

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

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

是否应该自动扫描scr / main / resources文件夹以查找复制到目标文件夹的资源和资源?

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

2 个答案:

答案 0 :(得分:2)

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

编辑: 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 /工件)中。排除的资源不会出现在类路径中,因此您无法通过运行Eclipse中的代码来访问它们。您只能从可执行jar中访问它们,并在清单文件中将targetPath文件夹指定为classpath。更好的选择是确保mave-jar-plugin和maven-resources-plugin。请参阅thisthis回答。

答案 1 :(得分:1)

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