Maven将属性文件添加到jar文件
Posted
技术标签:
【中文标题】Maven将属性文件添加到jar文件【英文标题】:Maven add properties file to jar file 【发布时间】:2014-07-22 03:55:36 【问题描述】:我正在使用以下插件配置来制作一个 jar 文件,并且我想在输出 jar 中的同一位置包含非 java src 文件,如 here 所述。
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>$java.version</source>
<target>$java.version</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
<!-- include non-java src files in the same place in the output jar -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
上面的不起作用,下面的也不起作用:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- include non-java src files in the same place in the output jar -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>$java.version</source>
<target>$java.version</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
【问题讨论】:
【参考方案1】:我是这样做的,并且运行良好。
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
【讨论】:
不要不要这样做。您在运行时从类路径加载的文件是资源,而不是 Java 代码。查看其他答案。【参考方案2】:在src/main/resources
而不是src/main/java
的同一包(文件夹)中添加文件或查看我的答案With maven - clean package, xml source files are not included in classpath
【讨论】:
以上是关于Maven将属性文件添加到jar文件的主要内容,如果未能解决你的问题,请参考以下文章