如何配置Maven以构建两个版本的工件,每个版本用于不同的目标JRE

Ale*_*ira 8 java target maven

我有一个maven模块,我需要在J2ME客户端和EJB服务器中使用.在客户端我需要为目标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>
Run Code Online (Sandbox Code Playgroud)

小智 6

正如海勒姆所建议的那样,您需要分两步完成,一步用于编译,另一步用于 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>
Run Code Online (Sandbox Code Playgroud)

然后对于 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>
Run Code Online (Sandbox Code Playgroud)

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

<dependency>
    <groupId>br.com.comp.proj</groupId>
    <artifactId>proj-cryptolib</artifactId>
    <version>0.0.4-SNAPSHOT</version>
    <classifier>jdk5</classifier>
</dependency>
Run Code Online (Sandbox Code Playgroud)


wja*_*ans 5

您可以通过Maven编译器插件进行配置.

看一下Maven编译器插件文档.

例如,您可以通过不同的配置文件启用它.

如果您只想拥有不同的目标版本,则只需使用变量目标即可.像这样的东西:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <source>1.3</source>
        <target>${TARGET_VERSION}</target>
        <fork>true</fork>
    </configuration>
 </plugin>
Run Code Online (Sandbox Code Playgroud)

  • 您可能希望添加,如果要同时构建两者,则需要使用分类器为打包的归档/可交付项指定备用名称. (3认同)