我正在创建一个超级罐子.jar即为我的项目提供依赖项。我有一堆项目使用的属性文件。我希望能够在运行项目之前更改这些属性文件,因此我希望它们位于 jar 之外。这是我的pom的相关部分
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<artifactSet>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.json</exclude>
</excludes>
</artifactSet>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>path.to.main.Main</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</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>
<include>**/*.json</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
所以本质上,我想创建一个文件夹${basedir}/target/conf
并将所有.properties
和.json
文件复制到其中。另外,这是我读取文件的方式
InputStream in = this.getClass().getClassLoader().getResourceAsStream("filename.properties");
我面临几个问题
当我做
mvn clean install
时,我仍然看到classes
文件夹中的所有.properties
和.json
文件。他们不应该被排除在外吗?conf
文件夹是使用所有文件创建的,但是当我运行 jar 并尝试更改属性时,不会选取更改。如何确保将conf
文件夹添加到类路径中?我希望能够在开发时从
src/main/resources
文件夹中加载.properties
和.json
文件,所以我不想将它们放在单独的文件夹中。这可能吗?
我遇到了同样的问题,即 Uber jar 无法读取外部配置文件。 我尝试了下面的配置,它就像魅力一样工作。请参阅下面的配置,它可能会帮助遇到 uber jar 无法读取 extenarl 文件问题的人。 我不确定这是否是最好的方法,但在网上没有找到任何灵魂:)
- 我已经使用IncludeResourceTransformer包含了资源。
- 使用过滤器从超级 jar 中删除了属性文件。
-
在类路径/conf 中,从外部文件夹读取属性。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <!-- Run shade goal on package phase --> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> add Main-Class to manifest file <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>JobName</Main-Class> <Class-Path>conf/</Class-Path> </manifestEntries> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer"> <resource>src/main/resources/config.properties</resource> <file>${project.basedir}/src/main/resources/config.properties</file> </transformer> </transformers> <finalName>FinalJarName</finalName> <filters> <filter> <artifact>groupId:artifactId</artifact> <excludes> <exclude>**/*.properties</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin>
祝你好运。