2011-10-23 109 views
3

我有一個由3個庫組成的項目 - 讓我們稱它們爲1)BABY,2)CHILD和3)ADULT。 Lib「CHILD」取決於「BABY」和「ADULT」取決於「CHILD」。Maven:庫的開發和發佈版本

我想要做的就是農產品:

  • 具有所有(傳遞)的一個開發版本依賴
  • 生產版本,每個庫(嵌入依賴)
  • 創建一個獨立的JAR

我有一個簡介dev和簡介release已經,我知道如何使用ProGuard來生成JAR。

問題是如何告訴Maven在dev中保留所有依賴關係並在production中忽略它們(可選/提供)?

回答

0

這是我最終使用:

  • 父POM定義配置文件release是configurates的proguard plugin(包裝箱一個大JAR)和install plugin(將釋放工件放入回購站中)。

  • lib-baby POM簡單地調用<build>部分中的2個插件。

  • lib-child POM還指定了一個dev配置文件,其中定義了對lib-baby的依賴關係。在發佈配置文件中,此依賴項具有optional標記,並且包含在大型JAR中。

在端時默認運行,com.company.dev:lib-babycom.company.dev:lib-child創建的庫(包括它們的依賴)。

當與-Prelease運行com.company:lib-babycom.company:lib-child創建的庫(獨立庫[無任何依賴性]) - 唯一的副作用是,默認工件(。* DEV)將被覆蓋:(

<project> 
    <groupId>com.company</groupId> 
    <artifactId>lib-parent</artifactId> 
    <packaging>pom</packaging> 

    <modules> 
     <module>lib-baby</module> 
     <module>lib-child</module> 
     <module>lib-adult</module> 
    </modules> 

    <profiles> 
     <profile> 
      <id>release</id> 
      <activation> 
       <property> 
        <name>release</name> 
       </property> 
      </activation> 

      <build> 
       <pluginManagement> 
        <plugins> 
         <!-- aggregate to one big jar --> 
         <plugin> 
          <groupId>com.pyx4me</groupId> 
          <artifactId>proguard-maven-plugin</artifactId> 
          <executions> 
           ... 
          </executions> 
          <configuration> 
           <injar>${project.build.finalName}.jar</injar> 
           <outjar>${project.build.finalName}-release.jar</outjar> 
           .... 
          </configuration> 
         </plugin> 

         <!-- install release version of artifact separately (under com.company) --> 
         <plugin> 
          <inherited>true</inherited> 
          <groupId>org.apache.maven.plugins</groupId> 
          <artifactId>maven-install-plugin</artifactId> 
          <executions> 
           <execution> 
            <id>install-release</id> 
            <goals> 
             <goal>install-file</goal> 
            </goals> 
            <configuration> 
             <file> 
              target/${project.build.finalName}-release.jar 
             </file> 
             <groupId>com.company</groupId> 
             ... 
            </configuration> 
           </execution> 
          </executions> 
         </plugin> 
        </plugins> 
       </pluginManagement> 
      </build> 
     </profile> 
    </profiles> 
</project> 

LIB-寶寶:

<project> 
    <groupId>com.company.dev</groupId> 
    <artifactId>lib-baby</artifactId> 
    <packaging>jar</packaging> 

    <parent> 
     <groupId>com.company</groupId> 
     <artifactId>lib-parent</artifactId> 
    </parent> 

    <profiles>  
     <profile> 
      <id>release</id> 

      <build> 
       <plugins> 
        <!-- produces 'lib-baby-release.jar --> 
        <plugin> 
         <groupId>com.pyx4me</groupId> 
         <artifactId>proguard-maven-plugin</artifactId> 
        </plugin> 

        <!-- installs 'lib-baby-release.jar --> 
        <plugin> 
         <groupId>org.apache.maven.plugins</groupId> 
         <artifactId>maven-install-plugin</artifactId> 
         <executions> 
          <execution> 
           <id>install-release</id> 
           <phase>install</phase> 
          </execution> 
         </executions> 
        </plugin> 
       </plugins> 
      </build> 
     </profile> 
    </profiles> 
</project> 

LIB-孩子:

<project> 
    <groupId>com.company.dev</groupId> 
    <artifactId>lib-child</artifactId> 
    <packaging>jar</packaging> 

    <parent> 
     <groupId>com.company</groupId> 
     <artifactId>lib-parent</artifactId> 
    </parent> 

    <profiles> 
     <profile> 
      <id>dev</id> 
      <activation> 
       <activeByDefault>true</activeByDefault> 
      </activation> 

      <dependencies> 
       <dependency> 
        <groupId>com.company.dev</groupId> 
        <artifactId>lib-baby</artifactId> 
       </dependency> 
      </dependencies> 
     </profile> 

     <profile> 
      <id>release</id> 

      <dependencies> 
       <dependency> 
        <groupId>com.company.dev</groupId> 
        <artifactId>lib-baby</artifactId> 
        <version>1.0</version> 
        <!-- made optional because will be embedded in standalone jar --> 
        <optional>true</optional> 
       </dependency> 
      </dependencies> 

      <build> 
       <plugins> 
        <!-- produces 'lib-child-release.jar --> 
        <plugin> 
         <groupId>com.pyx4me</groupId> 
         <artifactId>proguard-maven-plugin</artifactId> 
         <configuration> 
          <assembly> 
           <inclusions> 
            <inclusion> 
             <groupId>com.company.dev</groupId> 
             <artifactId>lib-baby</artifactId> 
            </inclusion> 
           </inclusions> 
          </assembly> 
         </configuration> 
        </plugin> 

        <!-- installs 'lib-child-release.jar --> 
        <plugin> 
         <groupId>org.apache.maven.plugins</groupId> 
         <artifactId>maven-install-plugin</artifactId> 
         <executions> 
          <execution> 
           <id>install-release</id> 
           <phase>install</phase> 
          </execution> 
         </executions> 
        </plugin> 
       </plugins> 
      </build> 
     </profile> 
    </profiles> 
</project> 
2

要在開發到部署時擁有不同的依賴關係,您可以使用maven配置文件。

http://maven.apache.org/guides/introduction/introduction-to-profiles.html

所以發展當你說「獨立罐子」這聽起來像你的意思是所有的依賴罐子合併到它,你會使用類似mvn -Pdev compile

時。

How can I create an executable JAR with dependencies using Maven?

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

+0

感謝回覆一層一層,但並不真正有幫助;我已經描述了幾乎所有你提到的事情(獨立的JAR,配置文件) - 這個問題可能會被吸引,添加我自己的答案(90%對此感到滿意) – stephanos