java - 如何配置 Maven 构建两个版本的 Artifact ,每个版本用于不同的目标 JRE

标签 java maven target

我有一个需要在 J2ME 客户端和 EJB 服务器中使用的 maven 模块。在客户端中,我需要为目标 1.1 编译它,在服务器中为目标 1.6 编译它。

我还需要将 1.6 版本部署到 Nexus 存储库,这样在服务器项目上工作的成员就可以包含此依赖项,而无需下载源代码。

我在 http://java.dzone.com/articles/maven-profile-best-practices 阅读过使用配置文件并不是最好的方法,但作者没有说什么是最好的方法。

这是我的 pom.xml:

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

    <parent>
        <artifactId>proj-parent</artifactId>
        <groupId>br.com.comp.proj</groupId>
        <version>0.0.4-SNAPSHOT</version>
    </parent>

    <artifactId>proj-cryptolib</artifactId>
    <name>proj - Cryto Lib</name>

    <dependencies>
        <dependency>
            <groupId>br.com.comp</groupId>
            <artifactId>comp-proj-mobile-messages</artifactId>
            <version>0.0.2-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>

        <plugins>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.3</source>
                    <target>1.1</target>
                    <fork>true</fork>
                </configuration>
            </plugin>

        </plugins>

    </build>

</project>

最佳答案

正如 Haylem 建议的那样,您需要分两步完成,一步用于编译,一步用于 jars。

对于编译器

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5</version>
    <executions>
      <execution>
        <configuration>
          <source>1.3</source>
          <target>1.5</target>
          <fork>true</fork>
          <outputDirectory>${project.build.outputDirectory}_jdk5</outputDirectory>
        </configuration>
      </execution>
      <execution>
        <configuration>
          <source>1.3</source>
          <target>1.6</target>
          <fork>true</fork>
          <outputDirectory>${project.build.outputDirectory}_jdk6</outputDirectory>
        </configuration>
      </execution>
    </executions>
 </plugin>

然后是jar插件

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.1</version>
        <executions>
          <execution>
            <goals>
              <goal>jar</goal>
            </goals>
            <configuration>
              <classesDirectory>${project.build.outputDirectory}_jdk5</classesDirectory>
              <classifier>jdk5</classifier>
            </configuration>
          </execution>
          <execution>
            <goals>
              <goal>jar</goal>
            </goals>
            <configuration>
              <classesDirectory>${project.build.outputDirectory}_jdk6</classesDirectory>
              <classifier>jdk6</classifier>
            </configuration>
          </execution>
        </executions>
      </plugin>

然后您可以通过添加 <classifier> 来引用所需的 jar元素到你的依赖。例如

<dependency>
    <groupId>br.com.comp.proj</groupId>
    <artifactId>proj-cryptolib</artifactId>
    <version>0.0.4-SNAPSHOT</version>
    <classifier>jdk5</classifier>
</dependency>

关于java - 如何配置 Maven 构建两个版本的 Artifact ,每个版本用于不同的目标 JRE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10920142/

相关文章:

html - CSS/HTML5 使用 :target or :active to display different text when link is clicked

java - jackson 冲突的属性(property)和 setter/getter 定义

maven - 如何使用 jacoco 检查多模块 Maven 项目的最小代码覆盖率?

jquery - 使用 JQuery,如何在 IE 中定位具有样式属性的元素?

html - 使用链接或 anchor 显示和隐藏内容

java - 将自定义注释处理器与 Checker Framework 一起使用

java - Hadoop 映射 : attach sources howto

java - 如何将表单的值放入对象并将对象发送到 Controller

java - spring读取属性文件时出现404错误

maven - 如何使用fabric8-maven-plugin标记图像?