Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring 4 @PropertySource relative path

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

like image 583
avalon Avatar asked Mar 31 '26 19:03

avalon


1 Answers

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>
like image 196
avalon Avatar answered Apr 03 '26 08:04

avalon