5

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

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

我可以通过将与单个子模块配置文件所需的相同依赖项应用于组装项目的配置文件来解决这个问题。但由于我必须在 POM 中重复自己,可能有一种更专业的方法来实现这一点。由于我对 Maven 很陌生,我想知道它是什么?我不想复制代码(由于代码保持不变,这甚至会重复更多)并且我不喜欢复制 POM 的某些部分,因为由于版本升级而更改它们可能会变得复杂。

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>

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

4

2 回答 2

3

有几种解决方案:

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

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

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

于 2011-10-04T16:12:22.503 回答
1

您可以将 maven 配置文件添加到 pom 并根据操作系统激活每个配置文件。配置文件激活确实支持这样的选项。然后在每个特定于操作系统的配置文件中,您可以列出可选的依赖项。这是一篇关于配置文件的文章-> http://maven.apache.org/guides/introduction/introduction-to-profiles.html

在你的情况下,它会是这样的:

<profiles>
  <profile>
    <activation>
      <os>
        <family>Windows</family>
      </os>
    </activation>
    <dependencies>
      ... specific dependencies for Windows
    </dependencies>
    <plugins>
      ... specific build plugins for Windows
    </plugins>
  </profile>
  <profile>
    <activation>
      <os>
        <family>unix</family>
      </os>
    </activation>
    <dependencies>
      ... specific dependencies for unix
    </dependencies>
    <plugins>
      ... specific build plugins for unix
    </plugins>
  </profile>
  <dependencies>
    ... Common dependencies
  <dependencies>
  <plugins>
    ... Common build plugins
  </plugins>
</profiles>
于 2011-10-04T16:14:28.403 回答