Search code examples
javamavenpom.xmlmaven-assembly-pluginexclude-constraint

maven-assembly-plugin: use config.properties during development and but pack config.default.properties as config.properties on jar-with-dependencies


I've got two configuration files (with usernames and passwords for the database for the testing phase) in src\main\resources\{config.properties,config.default.properties} During the development and my own testing I would like use src\main\resources\config.properties. On packaging the project, I'd like to include src\main\resources\config.default.properties as config.properties in the single jar with dependencies. How can I tell this directly in the single pom.xml? I tried to exclude src\main\resources\config.properties with this section, but even that didn't work:

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

Solution

  • You can define profile for environments

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

    Then you can specify the property to be used in maven command like mvn install -Pdev this will refer dev properties file.

    Then add your properties files to profiles/dev/config.properties and profiles/prod/config.properties in the project so that -Pdev can refer to the file. Finally add a plugin to copy the appropriate file to the resources:

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