Maven插件包含所有jar文件并排除所有配置文件

时间:2013-12-30 17:05:42

标签: maven maven-plugin maven-assembly-plugin maven-resources-plugin

我搜索了maven的插件/插件集,以包含jar文件中的所有依赖项,但资源文件夹中的配置文件除外。

我发现assembly插件是包含所有程序集的好解决方案,并且使用resourcesexclude标记,我已排除配置文件。

但问题是如何将这些文件复制到我的jar文件旁边的文件夹中?

换句话说,如果我想要一个jar(包括所有依赖项)和一个包含我的config(.xml,.properties文件)的文件夹,该怎么办?所以,当我想运行它时,我只需将config文件夹添加到类路径并运行jar文件。

谢谢,

P.S。如果您发现一个明确接受的答案的重复问题,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以通过在描述符XML文件中将格式设置为“dir”来从maven-assembly-plugin获取目录输出,如下所示:

<assembly>
    <formats>
        <format>dir</format>
    </formats>
    .....
</assembly>

然后使用dependencySets和fileSets包含或排除所需的文件。

要获取仅配置文件的目录,请使用fileSet仅将所需的文件包含到此新目录中。

您可以使用第二个程序集描述符来构建一个排除配置文件的jar,然后您可以在类路径中包含目录和jar。

要对同一插件执行两次,您可以执行以下操作:

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-assembly-plugin</artifactId>
     <executions>
         <execution>
             <id>config-dir</id>
             <configuration>
                 ....
             </configuration>
         </execution>
         <execution>
             <id>jar-without-config</id>
             <configuration>
                 ....
             </configuration>
         </execution>
     </executions>
 </plugin>

请点击此处了解更多详情:

http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

替代实施

这超出了你的问题范围,更适合另一个问题,但我认为更好的选择是使用像Spring这样的框架,允许你的外部配置文件覆盖你内部定位的文件,在这种情况下你可以使用默认的jar机制,不需要担心排除模板配置文件。例如,使用Spring,您可以执行以下操作:

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath*:/myproject/defaults.properties</value
            <value>file:${user.home}/myproject.properties</value>
        </list>
    </property>
</bean>

这将从jar加载内部'defaults.properties',然后从主目录加载'myproject.properties'。 myproject.properties中设置的任何值都将覆盖defaults.properties中设置的值。

我希望有所帮助。