0

我正在为我的项目创建一个uber jar,即具有依赖项的jar。我有一堆项目使用的属性文件。我希望能够在运行我的项目之前更改这些属性文件,所以我希望它们在jar之外。这是我的聚甲醛的有关章节Uber jar没有读取外部属性文件

<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 ADN试图改变属性,在更改不会拿起。我如何确保conf文件夹被添加到类路径中?

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

回答

0

我正面临着Uber jar没有读取外部配置文件的问题。 我试过下面的配置,它的工作就像魅力。请参阅下面的配置,它可以帮助有问题的人使用超级罐子不读取extenarl文件。 我不知道这是否是最好的方式,但没有发现任何soultion在线:)

  1. 我已经包括使用IncludeResourceTransformer的资源。
  2. 使用过滤器从uber jar中删除了属性文件。
  3. 在classpath/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> 
    

好运。