2015-08-30 24 views
0

我想創建一個可執行的jar(包含我的代碼的所有* .class)。 但是,我不希望Jar將編譯期間的資源包含在我的src/main/resources路徑中。Maven jar-with-dependencies without target classes(構建工件)

我的項目層次是:

project 
    -src 
    -main 

    -resources 
     -resources_setting.xml 
    -target 
    -classes 
     -resources_setting.xml 

我希望我的罐子裏只包括目標\類或內部資源內的主類和依賴關係,而不是資源。

我該怎麼做?

我使用行家組裝-插件,像這樣:

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
       <archive> 
        <manifest> 
         <mainClass>cqm.qa.Main</mainClass> 
        </manifest> 
       </archive> 
      </configuration> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>single</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
+0

以下是一個類似於您想要的解決方案的好方法。 http://stackoverflow.com/questions/3001713/where-in-maven-projects-path-should-i-put-configuration-files-that-are-not-cons/3004684#3004684 –

+1

_Why_你想要嗎?要做到這一點?如果這些文件不是你項目的資源,那麼你不應該把它們放在'src/main/resources'下。 – Tunaki

+0

我使用它們作爲資源,但我也在運行時編輯它們。所以它是有問題的找到並替換到一個XML文件,它的路徑是在一個jar文件(而不是目錄) – Matoy

回答

3

對於捆紮目的,我通常使用的maven-shade-plugin和設置如下所述。它將和程序集插件一樣工作。

<profile> 
    <id>generate-shaded-jar</id> 
    <activation> 
     <activeByDefault>false</activeByDefault> 
    </activation> 
    <build> 
     <resources> 
      <resource> 
       <directory>src/main/resources</directory> 
       <excludes> 
        <exclude>**</exclude> 
       </excludes> 
      </resource> 
     </resources> 
     <plugins>   
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-shade-plugin</artifactId> 
         <executions> 
          <execution> 
           <phase>package</phase> 
           <goals> 
            <goal>shade</goal> 
           </goals> 
           <configuration> 
            <transformers> 
             <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
              <manifestEntries> 
               <Main-Class>cqm.qa.Main</Main-Class> 
               <Class-Path>.</Class-Path> 
              </manifestEntries> 
             </transformer> 
            </transformers> 
            <filters> 
             <filter> 
              <artifact>*:*</artifact> 
              <excludes> 
               <exclude>log4j.properties</exclude> 
               <exclude>details.properties</exclude> 
               </excludes> 
             </filter> 
            </filters> 
           </configuration> 
          </execution> 
         </executions> 
       <configuration> 
        <finalName>cqm-full</finalName> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</profile> 

在上面的配置,我已經排除log4j.propertiesdetails.properties從最終的罐子依賴與名cqm-full.jar

更新

調用使用mvn install -Pgenerate-shaded-jar

現在資源配置文件來自src/main/resources的文件不會被添加到cqm-full.jar中。如果在沒有配置文件mvn clean install的情況下調用,您仍然可以查看罐子中的資源

+0

謝謝。以及如何排除*位於src/main/resources *下的*目錄*內容進入jar?不只是所有的* .xml和所有的* .properties .. *該路徑中的所有文件* – Matoy

+0

查看我上面更新的答案 – appu

+0

由於您刪除了所有'src/main/resources',因此可以避免'excludes'在上面的示例中配置 – appu

相關問題