Maven:如何将资源文件与jar一起放置?

时间:2021-12-06 14:14:00

I have \src\main\resources\logback.xml file. When I run mvn package it is placed into the jar by default. How do I make Maven place it next to jar, not inside?

我主要有\ src \ \ \ logback资源。xml文件。当我运行mvn包时,默认情况下将它放入jar中。如何让Maven放置在jar旁边,而不是在内部?

So, well, I just want Maven to copy the resource to the same folder where jar will be.

那么,我只是想让Maven将资源复制到jar所在的相同文件夹中。

3 个解决方案

#1


61  

No plugin definitions needed, just edit the build resources:

不需要插件定义,只需编辑构建资源:

<build>
    <resources>
        <!-- regular resource processsing for everything except logback.xml -->
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>logback.xml</exclude>
            </excludes>
        </resource>
        <!-- resource processsing with a different output directory
             for logback.xml -->
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>logback.xml</include>
            </includes>
            <!-- relative to target/classes
                 i.e. ${project.build.outputDirectory} -->
            <targetPath>..</targetPath>
        </resource>
    </resources>
</build>

Reference:

参考:

#2


1  

You can build a ZIP file containing both using the Maven Assembly plugin.

您可以构建一个包含两个使用Maven组装插件的ZIP文件。

You can also use Maven Antrun plugin or similar to put whatever file you want but the idea of single artifact per project is built deep into Maven internals so there is a chance it will hunt you down elsewhere.

您还可以使用Maven Antrun插件或类似的方式来放置您想要的任何文件,但是每个项目的单个工件的想法都是深入到Maven内部的,所以它有可能在其他地方找到您。

#3


1  

Exclude that file: http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

排除该文件:http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

And then use the Maven Antrun plugin to copy the file.

然后使用Maven Antrun插件来复制文件。

However, the latter part does not make much sense. If it is a configuration file to put in a server, simply manually copy it.

然而,后半部分没有多大意义。如果是将配置文件放入服务器,只需手动复制它。

#1


61  

No plugin definitions needed, just edit the build resources:

不需要插件定义,只需编辑构建资源:

<build>
    <resources>
        <!-- regular resource processsing for everything except logback.xml -->
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>logback.xml</exclude>
            </excludes>
        </resource>
        <!-- resource processsing with a different output directory
             for logback.xml -->
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>logback.xml</include>
            </includes>
            <!-- relative to target/classes
                 i.e. ${project.build.outputDirectory} -->
            <targetPath>..</targetPath>
        </resource>
    </resources>
</build>

Reference:

参考:

#2


1  

You can build a ZIP file containing both using the Maven Assembly plugin.

您可以构建一个包含两个使用Maven组装插件的ZIP文件。

You can also use Maven Antrun plugin or similar to put whatever file you want but the idea of single artifact per project is built deep into Maven internals so there is a chance it will hunt you down elsewhere.

您还可以使用Maven Antrun插件或类似的方式来放置您想要的任何文件,但是每个项目的单个工件的想法都是深入到Maven内部的,所以它有可能在其他地方找到您。

#3


1  

Exclude that file: http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

排除该文件:http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

And then use the Maven Antrun plugin to copy the file.

然后使用Maven Antrun插件来复制文件。

However, the latter part does not make much sense. If it is a configuration file to put in a server, simply manually copy it.

然而,后半部分没有多大意义。如果是将配置文件放入服务器,只需手动复制它。