Jan Bouchner
Jan Bouchner

Reputation: 897

Spring + Maven + .properties files

I use Spring and Maven to build my project and have two files located at src/main/resources path (application.properties, log4j.properties). If I have my project opened in IDE and run it, everything works fine, it is possible to change values in application.properties file and run project again with changed values. But when I run a command mvn clean install so yes, I get .jar file but I cannot configure this .jar with .properties file. This .jar uses values from properties file before it was builded.

I don't know if this matters but this is the way how I define properties file in Spring application context.

@PropertySource({"application.properties", "log4j.properties"})
public class AppConfig {

I jave these plugins inside pom.xml file. Is it ok? Can you help me please? It looks like builded .jar project does not know about .properties file at same directory level.

   <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.2</version>
            <!-- nothing here -->
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-4</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>cz.cvut.fit.bouchja1.ensemble.main.EnsembleApp</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>.</Class-Path> <!-- HERE IS THE IMPORTANT BIT -->
                    </manifestEntries>                        
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>                    
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

    </plugins>     

Upvotes: 2

Views: 3706

Answers (2)

Jan Bouchner
Jan Bouchner

Reputation: 897

Ok, it seems that solutions is this:

@PropertySource({"file:application.properties", "file:log4j.properties"})

After running mvn clean install and creating a .jar file, I can add application.properties and log4j.properties files beside .jar and I can configure the project with properties files.

BUT: When I want to run project from Netbeans, there is a problem that files "does not exist:... because it is not specified in classpath. I thought that this could be helpful:

@PropertySource({"classpath:log4j.properties", "classpath:application.properties", "file:application.properties", "file:log4j.properties"})

But not really. There an exception is thrown after this try:

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: cz.cvut.fit.bouchja1.ensemble.spring.AppConfig; nested exception is java.io.FileNotFoundException: application.properties (Directory or file does not exist)

EDIT: This is exactly the solution I was looking for How to Exclude poperties file from jar file?

Upvotes: 1

NimChimpsky
NimChimpsky

Reputation: 47280

For runtime properties make use of the Properties Api

Upvotes: 1

Related Questions