java - Maven 打包属性文件

标签 java maven maven-3 maven-jar-plugin

我有一个 Maven 多模块项目,分层如下。

test-integration 
   - test-integration.properties
test-services
   - test-services.properties
test-persistence
   - test-persistence.properties

每个项目都有一个属性文件。我应该如何配置maven以从jar中排除所有*.properties,同时将它们全部提取到配置文件夹中?

  • 测试集成/target/lib/test-integration.jar
  • test-integration/target/lib/test-service.jar
  • test-integration/target/lib/test-persistence.jar
  • 测试集成/target/config/test-integration.properties
  • test-integration/target/config/test-service.properties
  • test-integration/target/config/test-persistence.properties

最佳答案

您可以在聚合器/父项目中添加以下内容(以便将其应用于所有声明的模块):

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <excludes>
                    <exclude>**/test-*.properties</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/conf</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <includes>
                                    <include>test-*.properties</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

详细信息:

  • Maven Jar 插件从打包的 jar 中排除属性文件,并且还配置 target目录为 lib (如您在问题中所述)
  • Maven 资源插件正在复制到 target/conf目录(同样,如上所述)以 test- 开头并由 src/main/resources 提供的任何属性文件文件夹。

我在一个示例项目中进行了测试,效果很好。

另请注意:您的 IDE 可能会将 conf 目录显示为项目的一部分(将其视为新的目录资源,对于 Eclipse,这发生在我身上):这不是创建的新文件夹,而是与不同的看法。如果您确实不想产生这种副作用,请考虑使用 maven-antrun-plugin 而不是 Resources 插件。

关于java - Maven 打包属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34041789/

相关文章:

java - Android 旋转器 "onclicklistener"问题

java - 找不到 com.android.tools.build :gradle:2. 3.+ 的任何匹配项

java - Spring 启动,maven : failing to run or package an application

maven - 依赖项不存在于整个 POM 文件中

maven-plugin - 缺少 Maven 插件码头

maven - 从 Maven Central 安装 Maven 本身

java在内存分配方面的效率比较

java - 如何在 IntelliJ Idea 中为生成的单元测试添加默认导入

java - 正则表达式以匹配包含特定单词的所有URL

java - 在 Maven 构建的项目中启用断言