以Maven方式为不同平台构建

Ste*_*han 5 profiles maven maven-assembly-plugin

我有一个带子模块的项目a:jar,需要一组不同的依赖项,具体取决于它编译的平台.所有平台的代码都是相同的.例如,在Android上,httpcomponents库已经与操作系统捆绑在一起,而我必须将其包含在J2SE环境的构建中.我还有另一个子模块,它将几个子模块及其依赖项组装到一个存档中.如何可靠地配置程序集子模块,获取为各个平台编译的所有子模块及其适用于该平台的依赖项?

我尝试使用配置文件来创建a:jar:androida:jar:j2se.但是,声明对其中一个的依赖会导致程序集中出现奇怪的依赖关系.即,dependency:tree汇编项目有时会包含依赖关系a:jar:j2se(无论我是否声明在组件中使用a:jar:androidor a:jar:j2se),有时还包括另一个.在我更新a本地存储库中的jar后,它经常更改.切换装配项目还使用配置文件.

我可以通过将相同的依赖项应用于各个子模块配置文件所需的程序集项目的配置文件来解决这个问题.但是,由于我必须在POM中重复自己,所以可能还有更多的方法来实现这一目标.因为我对maven很新,我想知道它是什么?我不想复制代码(因为代码保持不变会更加重复)并且我不喜欢复制POM的部分因为版本升级而改变它们会变得复杂.

一些具体材料:来自a:jarPOM的依赖:

  <dependencies>
    .....
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpmime</artifactId>
      <version>4.0.1</version>
      <scope>compile</scope>
      <!-- Exclusion in the common part, they are provided in the profiles from different sources -->
      <exclusions>
        <exclusion>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpclient</artifactId>
        </exclusion>
        ....
      </exclusions>
    </dependency>
  </dependencies>
  <profiles>
    <profile>
      <id>android</id>
      <dependencies>
        <dependency>
          <groupId>com.google.android</groupId>
          <artifactId>android</artifactId>
          <version>1.6_r2</version>
          <scope>provided</scope>
        </dependency>   
      </dependencies>
    </profile>
    <profile>
      <id>java</id>
      <dependencies>
        <dependency>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpclient</artifactId>
          <version>4.0.1</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>commons-codec</groupId>
          <artifactId>commons-codec</artifactId>
          <version>1.3</version>
          <scope>compile</scope>
        </dependency>
      </dependencies>
    </profile>
  </profiles>
Run Code Online (Sandbox Code Playgroud)

汇编项目(使用Maven Assembly Plugin)配置文件:

  <profiles>
    <profile>
      <id>android</id>     
      <dependencies>
        <dependency>
          <groupId>a</groupId>
          <artifactId>a</artifactId>
          <version>${project.version}</version>
          <classifier>android</classifier>
          <type>jar</type>
        </dependency>
        <!-- Duplicate --> 
        <dependency>
          <groupId>com.google.android</groupId>
          <artifactId>android</artifactId>
          <version>1.6_r2</version>
          <scope>provided</scope>
        </dependency>   
        <!-- Duplicate --> 
      </dependencies>
    </profile>
    <profile>
      <id>java</id>
      <dependencies>
        <!-- Duplicate -->
        <dependency>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpclient</artifactId>
          <version>4.0.1</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>commons-codec</groupId>
          <artifactId>commons-codec</artifactId>
          <version>1.3</version>
          <scope>compile</scope>
        </dependency>
        <!-- /Duplicate --> 
        <dependency>
          <groupId>a</groupId>
          <artifactId>a</artifactId>
          <version>${project.version}</version>
          <classifier>java</classifier>
         <type>jar</type>
        </dependency>
      </dependencies>
    </profile>
  </profiles>
Run Code Online (Sandbox Code Playgroud)

我想摆脱标记的依赖声明.

kan*_*kan 3

有几种解决方案:

  1. 这些子模块的依赖项可以声明为“提供”,在这种情况下,在项目中,您可以包含对子模块的依赖项以及平台没有的显式依赖项。

  2. 用于<exclusions>不必要的依赖项。

  3. 使用上面的 (1) 或 (2) 创建另一个“结构”子模块 a-android:pom a-j2se:pom ,它仅描述依赖关系,并使用项目中的这些模块。