简体   繁体   English

在安装mvn之后,在JAR中将.properties文件包含.class文件

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

Imagine I have a project I want to build using maven. 想象我有一个要使用Maven构建的项目。 My project doesn't, unfortunately, respect the maven default layout. 不幸的是,我的项目没有遵循Maven的默认布局。 So I'm having two source folders A & B containing .properties files with .java sources. 所以我有两个源文件夹A & B其中包含带有.java源的.properties文件。

After an mvn install , my .properties files are not packaged in the jar with my .class generated files. mvn install之后,我的.properties文件不会与.class生成的文件一起打包在jar中。 Is there a way to do this automatically, you'd probably propose to use <resources> tag to solve this kind of problems; 有没有一种方法可以自动执行此操作,您可能会建议使用<resources>标记来解决此类问题。 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. 我知道这显然可行,但是我每次都必须指定.properties文件的存储位置以及我希望将它们打包在JAR中的位置,这对我来说不是解决方案,因为我有多个源文件夹(数百个) )定期更新。

<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 ? 对于每个源文件夹,我都可以将其写入到我的POM中,是否有人知道一种更简单的自动方法?

Regards, 问候,

Generally you can use Maven properties and profiles to solve this kind of problem. 通常,您可以使用Maven 属性配置文件来解决此类问题。

For example edit your pom to: 例如,将pom编辑为:

<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. 并在命令行中使用-Dname=value或在条件配置文件中或通过使用构建中可用的其他属性来定义myresources.directorymyresources.targetpath

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. 如果您解释了项目的结构和/或条件及其与变量(targetPath和目录)的关系,我也许可以为您的问题提供更多帮助。

而是在父pom中指定一次,所有子代都应继承相同的设置...

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? 将所有属性置于src / main / resources下的并行“包”层次结构中,将无法更改为使用标准Maven布局? Then you don't need to specify anything; 然后,您无需指定任何内容; all .properties files will be packaged. 所有.properties文件都将打包。

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. 至于您的多个源文件夹,则多模块Maven项目可能是最合适的,其中A和B是某些新父项目的子项目。

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. 这看起来可能需要做很多工作,但是Maven的约定相当合理,如果您反对这些约定,则往往会感到痛苦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM