I have a folder. And a jar app in this folder. Also I have a properties file in this folder. My conf. class is:
@Configuration
@Import({com.blabla.MyClass.class})
@ComponentScan(basePackages = "com.blabla")
**@PropertySource("file:///worker.core.properties")**
@EnableTransactionManagement
public class MainConfig {
@Autowired
private Environment env;
And this declaration does not sees my file, but it`s in the same directory. How to specify a relative path, for example like in @Import annotation? Thank you
So the solution is based on fact that we may extend classpath by specifying items in manifest file. So we need to
1) Leave properties file in /src/main/resources
2) Exclude it from a final jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
<archive>
<manifest>
<mainClass>com.blabla.daemon.MainListener</mainClass>
<classpathPrefix>lib/</classpathPrefix>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
3) Make a folder conf outside the jar
4) Copy there properties file from resources folder with maven
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
5) Specify manifest file that points to the conf folder (a part of step 1 -see there)
<archive>
<manifest>
<mainClass>com.blabla.daemon.MainListener</mainClass>
<classpathPrefix>lib/</classpathPrefix>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With