优步罐子不读取外部属性文件



我正在创建一个超级罐子.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");

我面临几个问题

  1. 当我做mvn clean install时,我仍然看到classes文件夹中的所有.properties.json文件。他们不应该被排除在外吗?

  2. conf文件夹是使用所有文件创建的,但是当我运行 jar 并尝试更改属性时,不会选取更改。如何确保将conf文件夹添加到类路径中?

  3. 我希望能够在开发时从src/main/resources文件夹中加载.properties.json文件,所以我不想将它们放在单独的文件夹中。这可能吗?

我遇到了同样的问题,即 Uber jar 无法读取外部配置文件。 我尝试了下面的配置,它就像魅力一样工作。请参阅下面的配置,它可能会帮助遇到 uber jar 无法读取 extenarl 文件问题的人。 我不确定这是否是最好的方法,但在网上没有找到任何灵魂:)

  1. 我已经使用IncludeResourceTransformer包含了资源。
  2. 使用过滤器从超级 jar 中删除了属性文件。
  3. 在类路径/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>
    

祝你好运。

最新更新