Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Different Properties Files for Runnable Jars, Maven Eclipse

I am new to Maven. I apologize if this is a silly question. I have a Maven Eclipse project with a pom file that creates a runnable jar using the shade plugin. My plan is to include one of several properties files in the build (please correct me if my terminology is incorrect. I really am green). I have multiple properties files located in src/main/resources.

I would like to find a way to include in the runnable jar only the properties file that applies. Specifically, I have two properties files (config1.properties, config2.properties) in my project and each build will only use one of these files depending on the desired functionality of the resulting jar. The properties file would be reassigned a generic name, specifically "defaultconfig.properties", so that it plays nicely with the code.

The resulting jar files (a different one for each different properties file) will run as separate cron jobs. I think I will be using different Jenkins projects to deploy the runnable jars to specific servers based on what task the properties file configures the project for (I think I got this part). The cron jobs will run on these servers.

my questions are:

1) where does the logic go that determines which properties file to include in the runnable jar?

for example, suppose I have config1.properties (that specifies to use var1 = blah and var2 = blip and var3 = boink) and config2.properties (which specifies to use var1 = duh var2 = dip and var3 = doink). Where do I tell Maven which of these two files to use for specific builds?

2) am I supposed to pass in a parameter that tells Maven which properties file to use? If so, who do I pass this parameter too? Is this something that can be configured in a Jenkins project? (even just some basic reading would help me here, as I am not sure if this would be a maven issue or a jenkins issue. I am green with both.)

Here is a the relevant portion of my pom.xml file:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.foo.dashboard.bar.Runner</mainClass>
                            </transformer>
                        </transformers>
                        <finalName>FooBar</finalName>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I have tried using IncludeResourceTransformer to rename the properties file and but was not sure how include the logic for switching properties files based on desired jar file functionality.

Thank you.

like image 970
miss.serena Avatar asked Nov 11 '22 13:11

miss.serena


1 Answers

This is what I came up with. I would love to know other approaches or the best way to do this however. Being totally new to all of this.

  1. Create separate executions in the same plugin. Note: They must be assigned different <id> values.
  2. Each <execution> can be configured differently. To rename the resources that you are including use the <transfomer>implementation = implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">.
  3. To exclude resources that are not needed (in my case, other properties files) use <transformer> implementation = "org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">. This is more nicety than necessity. Why bundle up unneeded files in your jar?
  4. Make sure each <execution> has a different <finalName> so that the resulting jars don't overwrite each other.

Here is my pom file.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>ID1</id>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.foo.dashboard.bar.Runner</mainClass>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                                <resource>defaultconfig.properties</resource>
                                <file>src/main/resources/defaultconfig_1.properties</file>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>defaultconfig_2.properties</resource>
                            </transformer>
                        </transformers>
                        <finalName>FooBar1</finalName>
                    </configuration>
                </execution>
                <execution>
                    <id>ID2</id>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.foo.dashboard.bar.Runner</mainClass>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                                <resource>defaultconfig.properties</resource>
                                <file>src/main/resources/defaultconfig_2.properties</file>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                <resource>defaultconfig_1.properties</resource>
                            </transformer>
                        </transformers>
                        <finalName>FooBar2</finalName>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I know this is not the final answer since this set up yields multiple jar files and I was originally looking for a solution that would give me only the jar file associated with a specific properties file, not all of the jar files from all of the properties files.

So I'm still taking suggestions on how to improve this solution so that the build would only a yield a single jar.

like image 118
miss.serena Avatar answered Nov 14 '22 23:11

miss.serena