0

I have logback.properties file which contains the following properties.

#logging
logging.server=
logging.port=
logging.path=/opt/${project.name}/logs

Inside my POM file I've got the <name> tag, which specifies the project name. I'd like to inject this name in few properties files like the lockback one above. Doing the above results in creating a folder called project.name_IS_UNDEFINED. Is it possible to access the project name and how?

UPDATE Ralph gave the correct answer, however check my comment, because for spring boot applications you need to used %project.name% instead of ${project.name}!

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Anton Belev
  • 11,963
  • 22
  • 70
  • 111

2 Answers2

2

You need to transfer the properties defined in you maven compile-time script into the application at run time.

The most easiest way is to use maven's resource filtering (the name is a bit misleading, it is not a filter that selects files, it is a property replace for text/resource files) in order to let maven replace ${project.name} in you logback.properties file.

true ${basedir}/src/main/resources

If you want enabling resource filtering just for one file (and not for the others to prevent maven from replacing other markers then you can use this snippet:

<resources>
    <!-- enable filtering for logback.properties only -->
    <resource>
        <filtering>false</filtering>
        <directory>${basedir}/src/main/resources</directory>
        <includes>
            <include>**/*</include>
        </includes>
        <excludes>
            <exclude>WHEREVER_LOCATED/logback.properties</exclude>
        </excludes>
    </resource>
    <resource>
        <filtering>true</filtering>
        <directory>${basedir}/src/main/resources</directory>
        <includes>
            <include>WHEREVER_LOCATED/logback.properties</include>
        </includes>
    </resource>
</resources>
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • Thanks, Ralph. Your answer helped me because I was missing the filtering :). However since I'm working on a spring boot application this did not fixed my problem fully. Instead I had to used %project.name% see here http://stackoverflow.com/questions/36501017/maven-resource-filtering-not-working-because-of-spring-boot-dependency. – Anton Belev Sep 21 '16 at 09:36
0

Try with the resource filtering: https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

gdegani
  • 846
  • 6
  • 17