【问题标题】:Maven plugin to include all jar files and exclude all config filesMaven插件包含所有jar文件并排除所有配置文件
【发布时间】:2013-12-30 17:05:41
【问题描述】:

我已经为 maven 搜索了一个插件/插件集,以将所有依赖项包含在一个 jar 文件中,除了资源文件夹中的配置文件。

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

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

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

谢谢,

附:如果您发现有明确接受的答案的重复问题,我将不胜感激。

【问题讨论】:

  • 不完全清楚你的目标是什么。你想要一个包含项目所有构建内容的目录,除了配置文件吗?或者你想从依赖 jar 中剥离一些文件?
  • 我想要一个 jar(包括所有依赖项)和一个包含我的配置(.xml、.properties 文件)的文件夹。所以,当我想运行它时,我只需将配置文件夹添加到类路径并运行 jar 文件。

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


【解决方案1】:

您可以通过在描述符 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 中设置的值。

希望对你有帮助。

【讨论】:

  • 我还是有问题。我正在使用 jar-with-dependencies descriptorRef。我不知道如何配置 jar-without-config 以包含每个依赖项并排除配置文件
  • 其实我并没有完全按照你说的使用,因为dir复制了src/main/resources中的资源。但是,使用 jar-without-config,不包括资源文件夹和 maven-resources-plugin,我得到了我想要的 :)
  • 您可以使用 config-dir 并排除 src/main/resources 中的文件,但我很高兴您能正常工作。感谢您接受我的回答。
猜你喜欢
  • 2019-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-16
  • 2012-09-10
  • 2015-09-14
  • 2014-06-26
  • 1970-01-01
相关资源
最近更新 更多