【问题标题】:maven-assembly-plugin: use config.properties during development and but pack config.default.properties as config.properties on jar-with-dependenciesmaven-assembly-plugin:在开发过程中使用 config.properties,但在 jar-with-dependencies 上将 config.default.properties 打包为 config.properties
【发布时间】:2021-08-27 12:29:28
【问题描述】:

我在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>

【问题讨论】:

    标签: java maven pom.xml maven-assembly-plugin exclude-constraint


    【解决方案1】:

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

         <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 这将引用 dev 属性文件。

    然后将你的属性文件添加到项目中的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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      相关资源
      最近更新 更多