maven - 以 Maven 方式为不同平台构建

标签 maven maven-assembly-plugin profiles

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

我尝试使用配置文件创建 a:jar:androida:jar:j2se .但是,声明对其中之一的依赖会导致程序集中出现奇怪的依赖关系。即 dependency:tree的装配项目有时会包含对 a:jar:j2se 的依赖项。 (不管我是否声明在程序集中使用 a:jar:androida:jar:j2se),有时是另一个。在我更新了 a 的 jar 之后,它(经常)发生了变化在本地存储库中。在装配项目中切换也使用配置文件。

我可以解决 by applying the same dependencies to the assembly project's profiles根据各个子模块配置文件的需要。但由于我必须在 POM 中重复自己,可能有一种更专业的方法来实现这一点。由于我对 Maven 很陌生,我想知道它是什么?我不想复制代码(由于代码保持不变,这甚至会重复更多)并且我不喜欢复制 POM 的某些部分,因为由于版本升级而更改它们可能会变得复杂。

一些具体的 Material :来自 a:jar 的依赖项的POM:

  <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>

组装项目(使用 Maven 组装插件)配置文件:
  <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>

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

最佳答案

有几种解决方案:

  • 这些子模块的依赖关系可以声明为“提供”,在这种情况下,在项目中,您包括对子模块的依赖关系以及平台没有的显式依赖关系。
  • 使用<exclusions>对于不必要的依赖。
  • 使用上面的 (1) 或 (2) 创建另一个“结构”子模块 a-android:pom a-j2se:pom 仅描述依赖关系,并使用项目中的这些模块。
  • 关于maven - 以 Maven 方式为不同平台构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7650727/

    相关文章:

    maven - Gradle 将 Artifact 部署到本地目录中的 Maven 存储库

    java - 使用 Eclipse 和 Maven 运行 SpringBoot JUnit-Tests 时的不同结果

    mysql - 为用户帐户编写系统

    java - Sonar 管 : "Project configuration file: NONE "

    java - Jenkins Maven : How to create independent jobs with single POM file

    java - maven- assembly-plugin 锁定文件

    maven-2 - 生成具有不同/过滤类的多个 jar 的 Maven 最佳实践?

    java - Maven 程序集的替代方案 :single for packaging

    gradle - 如何根据配置文件将文件复制到WAR中?

    php - Codeigniter Auth Library - 特定组的用户名公共(public)配置文件,对其他组禁用