Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: properties-file alongside jar

I am packaging my application as jar.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>...</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

And also I have my .properties-file placed inside of src/main/resources. I can freely move this file to any other place. I don't want properties-file to be included into jar, but rather placed into the same output directory (where we get jar) side-by-side. What is the best practice for this in Maven?

like image 812
Alec Avatar asked Aug 06 '13 10:08

Alec


People also ask

Does jar include properties file?

When you build the JAR file, just include it together with your class files. You can use the method getResourceAsStream() in class Class or ClassLoader to read your properties file inside the JAR file.

How to include properties file in jar using Maven?

Create a properties file to store Maven configuration values for the project. Add the Maven Properties Plugin to the Project's POM file. Add the WebLogic Maven Plugin to project's POM file. Add JUnit tests and JUnit dependencies to project.


1 Answers

Okay, one can use goal resources:copy-resources of maven-resources plugin. I am just leaving standard resources folder for embedded (into jar) resources. And for external resource I create another folder: src/main/external-resources. Here I put my .properties-file.

Following piece will copy external resources to output dir upon validate phase. You can alter the phase per your own needs.

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/external-resources</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
like image 58
Alec Avatar answered Oct 15 '22 17:10

Alec