【问题标题】:How to modify mvn.cmd to get the correct jdk build version in MANIFEST.MF file如何修改 mvn.cmd 以在 MANIFEST.MF 文件中获取正确的 jdk 构建版本
【发布时间】:2018-07-12 17:55:29
【问题描述】:

我在我的机器上安装了 2 个版本的 java,1.7 和 1.8。 为了构建我的 java 项目,我使用的是 maven 3.5.0。

在某些情况下,我必须使用 java 1.7 构建我的 java 项目, 所以我将我的%JAVA_HOME% 环境变量从"C:\Program Files\Java\jdk1.8.0_131" 更改为"C:\Program Files\Java\jdk1.7.0_80"

然后我想如果我能做到,那 pom.xml 决定了 java 的版本,应该用它来构建项目。

一开始我的 pom.xml 是这样的

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
    </plugin>
...
</plugins>

如您所见,有 &lt;source&gt;&lt;target&gt; 标签,但此标签不适用于 java 1.7、1.8,也许它适用于早期版本。

所以我不得不在 Maven 的“settings.xml”和“pom.xml”文件中进行一些更改:

settings.xml

<profiles>
    <profile>
        <id>compiler</id>
          <properties>
            <JAVA_1_7_HOME>C:\Program Files\Java\jdk1.7.0_80\bin\javac</JAVA_1_7_HOME>
            <JAVA_1_8_HOME>C:\Program Files\Java\jdk1.8.0_131\bin\javac</JAVA_1_8_HOME>
          </properties>
    </profile>
</profiles>

<activeProfiles>
        <activeProfile>compiler</activeProfile>
</activeProfiles>

pom.xml

<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <executable>${JAVA_1_7_HOME}</executable>
                <verbose>true</verbose>
                <fork>true</fork>               
            </configuration>
        </plugin>
...
</plugins>

然后使用mvn install 进行构建,它成功了!!!如果我将可执行文件更改为 ${JAVA_1_8_HOME} 生成的 jar 文件的大小会发生变化。

但是 MANIFEST.MF 中有一个大问题。 JDK的构建版本是1.8.0_161,所以MANIFEST.MF会骗人,想知道构建jdk的版本。

这是因为 Maven(mvn.cmd 文件)查找 %JAVA_HOME% 并采用 java 的路径。如果环境中没有 %JAVA_HOME% 变量,则采用默认系统 java -version,在我的情况下为 1.8.0_161(JRE 版本)。

这里是mvn.cmd 代码 sn-p

@REM ==== START VALIDATION ====
if not "%JAVA_HOME%"=="" goto OkJHome
for %%i in (java.exe) do set "JAVACMD=%%~$PATH:i"
goto checkJCmd

现在是一个挑战

如何告诉mvn.cmd它是用pom.xml编写的java 7构建的?

如何让mvn.cmd在MANIFEST.MF文件中写入正确的构建jdk?

【问题讨论】:

    标签: java xml maven pom.xml


    【解决方案1】:

    这是解决方案,如何修复 MANIFEST.MF 文件中不正确的 JDK 版本。此修复将永远不会让您错误地构建项目(如果您的配置正确)。

    首先需要从环境PATH中删除Java链接C:\Program Files\Java\jdk1.7.0_80\bin%java_home%\bin

    然后你必须在 maven settings.xml 文件中添加新的配置文件

    settings.xml

    <profile>
      <id>buildByJava7</id>
      <activation>
        <property>
          <name>java</name>
          <value>7</value>
        </property>
      </activation>
      <properties>
        <java-version>1.7</java-version>
        <java-1.7>C:\Program Files\Java\jdk1.7.0_80\bin\javac</java-1.7>
        <jdk>1.7.0_80</jdk>
        <wsimport>C:\Program Files\Java\jdk1.7.0_80\bin\wsimport.exe</wsimport>
      </properties>
    </profile>
    
    <profile>
      <id>buildByJava8</id>
      <activation>
        <property>
          <name>java</name>
          <value>8</value>
        </property>
      </activation>
      <properties>
         <java-version>1.8</java-version>
         <java-1.8>C:\Program Files\Java\jdk1.8.0_131\bin\javac</java-1.8>
         <jdk>1.8.0_131</jdk>
         <wsimport>C:\Program Files\Java\jdk1.8.0_131\bin\wsimport.exe</wsimport>
      </properties>
    </profile>
    

    如您所见,有全局属性&lt;java-version&gt;, &lt;java-1.8&gt;, &lt;jdk&gt;, &lt;wsimport&gt;。您可以使用mvn -Djava=7mvn -Djava=8 命令激活配置文件

    现在你必须以这种方式更改 Pom.xml

    pom.xml

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <verbose>true</verbose>
        <fork>true</fork>
        <source>${java-version}</source>  <!-- java compile version -->
        <target>${java-version}</target>  <!-- java compile version -->
        <executable>${java-1.8}</executable>  <!-- ..\bin\javac.exe file path -->
      </configuration>
    </plugin>
    

    ....

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
          </manifest>
          <manifestEntries>
            <Build-Jdk>${jdk}</Build-Jdk>  <!-- jdk version to set in manifest.mf file -->
          </manifestEntries>
        </archive>
      </configuration>
    </plugin>
    

    ...

    如果您使用 wsimport 从 *wsdl 生成类,您必须添加带有 wsimport.exe 文件路径的可执行文件。 请预见,如果您在 %PATH% 中有 java 链接,这将不起作用

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>jaxws-maven-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <goal>wsimport</goal>
          </goals>
          <configuration>
            <wsdlDirectory>${basedir}/src/main/resources/wsdl/app</wsdlDirectory>
            <packageName>ge.jibo.app.client</packageName>
            <sourceDestDir>${basedir}/target/generated-sources</sourceDestDir>
            <keep>true</keep>
            <bindingFiles>
              <bindingFile>${basedir}/src/main/resources/wsdl/app/binding.xml</bindingFile>
              <bindingFile>${basedir}/src/main/resources/wsdl/app/jaxb-binding.xml</bindingFile>
            </bindingFiles>
            <verbose>true</verbose>
            <executable>${wsimport}</executable>  <!-- ...\bin\wsimport.exe file path -->
          </configuration>
        </execution>
      </executions>
    </plugin>
    

    修改后你可以直接运行命令mvn -Djava=8 clean package

    此命令将激活buildByJava8 配置文件。 pom.xml 会从 profile 中获取所有的 java 版本和 java/wsimport 路径,一切都会被编译成功并正确构建。

    【讨论】:

      【解决方案2】:

      我刚刚尝试使用 maven-archiver 插件来强制自定义 MANIFEST.MF 并以某种方式确定“Build-Jdk”条目。但它似乎总是覆盖 java 版本。如果您想尝试一下,请查看https://maven.apache.org/shared/maven-archiver/examples/manifestFile.html

      我不认为更改 maven.cmd 可以让这个脚本意识到项目的特殊性。

      如果重点是避免在您的某些构建之前手动更改 JAVA_HOME,那么您可能只需要为 maven 制作一个包装批处理文件 引用你的 jdk1.7:

      SET "JAVA_HOME=C:\Program Files\Java\jdk1.7.0_80"
      mvn.cmd %*
      

      将此批处理文件另存为mvn_j7.bat 在您的[MAVEN_HOME]\bin 文件夹中。然后您可以在任何地方运行它,如下例所示:

      mvn_j7 clean package
      

      【讨论】:

      • 感谢您的建议
      猜你喜欢
      • 2023-01-12
      • 1970-01-01
      • 1970-01-01
      • 2019-06-11
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 2014-07-10
      • 2010-11-20
      相关资源
      最近更新 更多