maven-assembly-plugin:在开发过程中使用 config.properties,但在 jar-with-dependencies

问题描述

我在 src\main\resources\{config.properties,config.default.properties} 中有两个配置文件(带有用于测试阶段的数据库用户名和密码)在开发和我自己的测试期间,我想使用 src\main\resources\config.properties。在打包项目时,我想将 src\main\resources\config.default.properties 作为 config.properties 包含在具有依赖项的单个 jar 中。我怎样才能在单个 pom.xml 中直接说出这一点?我试图用这个部分排除 src\main\resources\config.properties,但即使这样也不起作用:

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.3.0</version>

            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.great.tech.Client</mainClass>
                        <packageName>com.great.tech.client</packageName>
                        <addClasspath>true</addClasspath>
                    </manifest>
                    <manifestEntries>
                        <Created-By>Great Technologies AG</Created-By>
                        <Version>${project.version}</Version>
                        <Title>Great Tech</Title>
                    </manifestEntries>
                </archive>
                <appendAssemblyId>false</appendAssemblyId>

                <dependencySets>
                    <dependencySet>
                        <excludes>
                            <exclude>**/config.properties</exclude>
                        </excludes>
                    </dependencySet>
                </dependencySets>
            </configuration>

            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>

        </plugin>

解决方法

您可以为环境定义配置文件

     <profile>
        <id>dev</id>
        <properties>
            <activatedProperties>dev</activatedProperties>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <activatedProperties>prod</activatedProperties>
        </properties>
    </profile>

然后您可以指定要在 maven 命令中使用的属性,例如 mvn install -Pdev 这将引用开发属性文件。

然后将您的属性文件添加到项目中的 profiles/dev/config.propertiesprofiles/prod/config.properties,以便 -Pdev 可以引用该文件。最后添加一个插件将相应的文件复制到资源中:

        <plugin>
            <groupId>com.coderplus.maven.plugins</groupId>
            <artifactId>copy-rename-maven-plugin</artifactId>
            <version>1.0</version>
            <executions>
                <execution>
                    <id>copy-file</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                        <configuration>
                            <sourceFile>profiles/${build.profile.id}/config.properties</sourceFile>
                            <destinationFile>src/main/resources/config.properties</destinationFile>
                        </configuration>
                </execution>
            </executions>
        </plugin>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...