如何解决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.properties
和 profiles/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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。