2014-06-06 37 views
1

我工作的結構爲多模塊Maven項目如下:Maven的:如何將模塊依賴關係到普通依賴性的罐子文件夾

SQR 
+ pom.xml 
+ core 
| + src 
| + target 
| | + dependency-jars 
| pom.xml 
+ connector 
| + src 
| pom.xml 
+ xmlreader 
| + src 
| pom.xml 
+ xmlwriter 
| + src 
| pom.xml 

的SQR是頂級項目,而核心,連接器,xmlreader,xmlwriter是模塊。目前,核心將所有東西一起構建成一個帶有外部jar庫的可執行jar文件。核心使用幾個依賴關係,即log4j,commons。到現在爲止還挺好。出現問題的模塊正在使用特定的依賴關係,即http-client,commons-io。它們都被添加到類路徑中,但它們不會被複制到core/target/dependency-jar。另一個缺點是,我必須在使用依賴項時擴展每個模塊的pom.xml(例如複製依賴關係等)。

目前,我有以下文件:

SQR/pom.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.sqr</groupId> 
    <artifactId>SQR</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>pom</packaging> 

    <name>SQR</name> 
    <url>http://maven.apache.org</url> 
    <modules> 
     <module>core</module> 
     <module>connector</module> 
     <module>xmlwriter</module> 
     <module>xmlreader</module> 
    </modules> 
</project> 

核心/ pom.xml中:

<?xml version="1.0"?> 
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <modelVersion>4.0.0</modelVersion> 
    <parent> 
    <groupId>com.sqr</groupId> 
    <artifactId>SQR</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    </parent> 
    <artifactId>core</artifactId> 
    <packaging>jar</packaging> 
    <name>core</name> 
    <url>http://maven.apache.org</url> 
    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 
    <dependencies> 
    [...] 
    <dependency> 
     <groupId>org.apache.commons</groupId> 
     <artifactId>commons-lang3</artifactId> 
     <scope>compile</scope> 
    </dependency> 
    </dependencies> 

    <build> 
    <plugins> 
     [...] 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-dependency-plugin</artifactId> 
      <version>2.5.1</version> 
      <executions> 
       <execution> 
        <id>copy-dependencies</id> 
        <phase>package</phase> 
        <goals> 
         <goal>copy-dependencies</goal> 
        </goals> 
        <configuration> 
         <includeGroupIds>com.sqr, org.apache.commons</includeGroupIds> 
         <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

其他模塊的pom.xml文件看起來與上面列出的類似。它感覺像很多開銷,擴展每個pom.xml文件。有沒有解決這個問題的最佳做法?還是有這個問題的快速和乾淨的修復?

tl; dr:我想要一個多模塊項目,其中所有模塊及其依賴項都被構建爲單獨的.jar文件並鏈接在一起。如下:

+ dependency-jars 
| commons-lang3-3.3.2.jar (used by only xmlwriter) 
| connector-1.0-SNAPSHOT.jar 
| xmlreader-1.0-SNAPSHOT.jar 
| xmlwriter-1.0-SNAPSHOT.jar 
| log4j-1.2.17.jar (used by all modules) 
core-1.0-SNAPSHOT.jar (being the main entry of the application) 

回答

2

解決您的問題似乎是一個分佈。

以下解決方案將幫助你建立一個分佈神器:

  • 啓動/ core.jar添加
  • 庫/ connector.jar
  • 庫/ xmlwriter.jar
  • 庫/ XMLReader的。罐子

不用擔心它聽起來更簡單!在目錄distribution.xml:

  1. 創建一個名爲ditrib新模塊

  2. 創建彙編文件的src/main /裝配。

內容應該關閉這一個:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">` 

<id>distrib</id> 

<formats> 
    <format>jar</format> 
</formats> 

<includeBaseDirectory>false</includeBaseDirectory> 

<dependencySets> 
    <dependencySet> 
     <outputDirectory>/boot</outputDirectory> 
     <includes> 
      <include>${project.groupId}:core</include> 
     </includes> 
    </dependencySet> 
    <dependencySet> 
     <outputDirectory>/libs</outputDirectory> 
     <includes> 
      <include>${project.groupId}:connector</include> 
      <include>${project.groupId}:xmlwriter</include> 
      <include>${project.groupId}:xmlreader</include> 
      <include>*:commons-lang3</include> 
      <include>*:log4j</include> 
     </includes> 
    </dependencySet> 
</dependencySets> 

</assembly> 
  1. 內,您的POM,創建分發然後聲明:

     <plugin> 
         <artifactId>maven-assembly-plugin</artifactId> 
         <configuration> 
          <descriptors> 
           <descriptor>src/main/assembly/distrib.xml</descriptor> 
          </descriptors> 
          <finalName>${project.artifactId}-${project.version}</finalName> 
          <appendAssemblyId>false</appendAssemblyId> 
         </configuration> 
         <executions> 
          <execution> 
           <id>make-assembly</id> 
           <phase>package</phase> 
           <goals> 
            <goal>single</goal> 
           </goals> 
          </execution> 
         </executions> 
        </plugin> 
    

這將產生在發出以下命令後,目標文件夾中名爲* -distrib.jar的jar:mvn packa GE。

注意:一般來說,發行版會附帶一個config/config.properties文件,它在引導目錄中的內容和libs目錄中的內容之間進行粘合。如果沒有bin/run {sh | bat}文件,分發也無法完全實現。

我希望我幫你! 乾杯!

+0

您是否建議使用您建議的文件和內容創建另一個模塊,即'distro'?還是應該調整核心模塊的pom.xml?謝謝! – Velth

+0

是的,我建議添加一個模塊 – thesmash

0

看看Maven的Shade插件,也許它可能會有所幫助。

http://maven.apache.org/plugins/maven-shade-plugin/

+0

是的,雖然我不想創建一個超級罐子。我想在我的dependency-jar /文件夾中分開.jar文件。就像我在文章中所述。不管怎麼說,還是要謝謝你。 – Velth

2

如果您的目標是將連接器,xmlreader和xmlwriter jar以及它們的所有依賴包括到核心模塊的dependencies文件夾中,那麼常用的方法是將這三個模塊列爲編譯依賴關係核心模塊pom.xml,就像核心模塊是依賴較低級別模塊(如域或通用模塊)的Web模塊一樣。

這些模塊不會出現在列出的核心模塊pom.xml的依賴項部分。添加它們可能無法爲您的問題提供100%的解決方案,因爲正在執行定製打包,但應該使您更接近目標。

編輯:刪除

<includeGroupIds>com.sqr, org.apache.commons</includeGroupIds> 

,那麼所有的傳遞依賴應該被複制的連接器,XMLReader的,XmlWriter的。

+0

我沒有複製/粘貼完整的依賴項列表。但實際上,我的core/pom.xml對模塊xmlreader/xmlwriter和連接器都有3個依賴關係。但我仍然需要手動將其他模塊的依賴關係添加到我的core/pom.xml中。我想通過包裝(我猜..?)自動化這個。 – Velth

+0

請參閱編輯以不限制正在複製的傳遞依賴關係。 –