开发者

Including .properties files with .class in JAR after an mvn install

开发者 https://www.devze.com 2023-01-31 06:14 出处:网络
Imagine I have a project I want to build using maven. My project doesn\'t, unfortunately, respect the maven default layout. So I\'m having two source folders A & B containing .properties files wit

Imagine I have a project I want to build using maven. My project doesn't, unfortunately, respect the maven default layout. So I'm having two source folders A & B containing .properties files with .java开发者_高级运维 sources.

After an mvn install, my .properties files are not packaged in the jar with my .class generated files. Is there a way to do this automatically, you'd probably propose to use <resources> tag to solve this kind of problems; this obviously works I know, but i'll have to each time specify where my .properties files are stored, and where I want them to be packaged in the JAR, this is not a solution for me since I have multiple source folders (hundreds) regularly updated.

<resource>
        <targetPath>com\vermeg\jar2</targetPath>
        <filtering>true</filtering>
        <directory>${basedir}/a/jar2</directory>
        <includes>
          <include>*.properties</include>
        </includes>
        <excludes>
          <exclude>*.java</exclude>
        </excludes>
</resource>

Will I have, for each source folder, to write this in my POM, does anyone knows a simpler automatic way to do this ?

Regards,


Generally you can use Maven properties and profiles to solve this kind of problem.

For example edit your pom to:

<resource>
    <targetPath>${myresources.targetpath}</targetPath>
    <directory>${myresources.directory}</directory>
    ...
</resource>

and define myresources.directory and myresources.targetpath in command line using -Dname=value or in conditional profile or by using other properties available in your build.

If you explain your project structure and/or conditions and their relation to your variable (targetPath and directory) I may be able to help more with your question.


Specify it once in a parent pom instead, and all the children should inherit the same setup...


Would it be impossible to change to using the standard Maven layout, with all your properties in a parallel 'package' hierarchy under src/main/resources? Then you don't need to specify anything; all .properties files will be packaged.

If you do this, you'll need to enable filtering as it's off by default. You may have to explicitly declare the resource directories again, as when you declare them, this seems to override the ones you get 'for free'.

As for your multiple source folders, a multi-module Maven project would probably be the best fit, with A and B being children of some new parent project.

This might seem like quite a lot of work, but Maven's conventions are fairly reasonable, and it tends to be a painful experience if you go against them.

0

精彩评论

暂无评论...
验证码 换一张
取 消