如何修改mvn.cmd以在MANIFEST.MF文件中获取正确的jdk构建版本

时间:2021-10-31 06:28:56

I have installed 2 version of java on my machine, 1.7 and 1.8. for building my java projects I'm using maven 3.5.0.

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

There are some cases when I have to build my java project using java 1.7, So I'm changing my %JAVA_HOME% environment variable to "C:\Program Files\Java\jdk1.7.0_80" from "C:\Program Files\Java\jdk1.8.0_131".

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

Then I thought if I can make so, that pom.xml determine the version of java, by which the project should be build.

然后我想如果我能这样做,pom.xml确定java的版本,通过它来构建项目。

At first my pom.xml was looked like this

起初我的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>

as you can see there is <source> and <target> tags but this tags does not works for java 1.7,1.8, maybe it worked for earlier versions.

正如你所看到的那样有 标签,但这个标签不适用于java 1.7,1.8,也许它适用于早期版本。

So I had to make some changes in Mavens "settings.xml" and in "pom.xml" files:

所以我不得不在Mavens“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>

Then Make build using mvn install and it worked!!! If I changed executable to ${JAVA_1_8_HOME} the size of generated jar file changes.

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

But there is one Big Issue in MANIFEST.MF. the build version of JDK is 1.8.0_161, so MANIFEST.MF will lie someone, who want to find out build jdk version.

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

The reason of this is that Maven (mvn.cmd file) looks to %JAVA_HOME% and takes the path of java. if there is no %JAVA_HOME% variable in environment, it takes the default system java -version which is in my case 1.8.0_161 (JRE version).

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

here is the mvn.cmd code snippet

这是mvn.cmd代码片段

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

Now here is a challenge

现在这是一个挑战

how to tell mvn.cmd that it was build by java 7 which is written in pom.xml?

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

how to make mvn.cmd to write the correct build jdk in MANIFEST.MF file?

如何让mvn.cmd在MANIFEST.MF文件中编写正确的build jdk?

2 个解决方案

#1


2  

Here's solution, how to fix incorrect JDK version in MANIFEST.MF file . This fix will never let you to build your project incorrectly (if your configuration will correct).

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

At first it is necessary to remove Java link C:\Program Files\Java\jdk1.7.0_80\bin or %java_home%\bin from environment PATH.

首先,必须从环境PATH中删除Java链接C:\ Program Files \ Java \ jdk1.7.0_80 \ bin或%java_home%\ bin。

Then you have to add new Profiles in maven settings.xml file

然后,您必须在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>

As you can see there is global Properties <java-version>, <java-1.8>, <jdk>, <wsimport>. you can activate Profile using mvn -Djava=7 or mvn -Djava=8 command

如您所见,有全局属性 , , , 。您可以使用mvn -Djava = 7或mvn -Djava = 8命令激活配置文件

Now you have to change Pom.xml in this way

现在你必须以这种方式更改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>

...

in case if you are using wsimport to generate classes from *wsdl, you have to add executable with wsimport.exe file path. please foresee, that this will not work if you have java link in a %PATH%

如果您使用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>

After this modification you can just run command mvn -Djava=8 clean package

在此修改之后,您只需运行命令mvn -Djava = 8 clean package

This command will activate buildByJava8 profile. pom.xml will get all java versions and java/wsimport paths from profile, and everything will be compiled and build successfully and correctly.

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

#2


1  

I've just tried to use maven-archiver plugin to force a custom MANIFEST.MF and somehow determine the "Build-Jdk" entry. But it seems to always override the java version. If you'd like to have a try, check https://maven.apache.org/shared/maven-archiver/examples/manifestFile.html

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

I don't think changing maven.cmd can make this script aware of the project peculiarities.

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

If the point is to avoid changing JAVA_HOME manually before some of your builds, maybe all you need is to make a wrapper batch file for maven referencing your jdk1.7:

如果要避免在某些构建之前手动更改JAVA_HOME,可能只需要为maven引用jdk1.7创建一个包装批处理文件:

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

Save this batch file as mvn_j7.bat inside your [MAVEN_HOME]\bin folder.Then you can run it anywhere, just like in the example below:

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

mvn_j7 clean package

#1


2  

Here's solution, how to fix incorrect JDK version in MANIFEST.MF file . This fix will never let you to build your project incorrectly (if your configuration will correct).

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

At first it is necessary to remove Java link C:\Program Files\Java\jdk1.7.0_80\bin or %java_home%\bin from environment PATH.

首先,必须从环境PATH中删除Java链接C:\ Program Files \ Java \ jdk1.7.0_80 \ bin或%java_home%\ bin。

Then you have to add new Profiles in maven settings.xml file

然后,您必须在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>

As you can see there is global Properties <java-version>, <java-1.8>, <jdk>, <wsimport>. you can activate Profile using mvn -Djava=7 or mvn -Djava=8 command

如您所见,有全局属性 , , , 。您可以使用mvn -Djava = 7或mvn -Djava = 8命令激活配置文件

Now you have to change Pom.xml in this way

现在你必须以这种方式更改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>

...

in case if you are using wsimport to generate classes from *wsdl, you have to add executable with wsimport.exe file path. please foresee, that this will not work if you have java link in a %PATH%

如果您使用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>

After this modification you can just run command mvn -Djava=8 clean package

在此修改之后,您只需运行命令mvn -Djava = 8 clean package

This command will activate buildByJava8 profile. pom.xml will get all java versions and java/wsimport paths from profile, and everything will be compiled and build successfully and correctly.

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

#2


1  

I've just tried to use maven-archiver plugin to force a custom MANIFEST.MF and somehow determine the "Build-Jdk" entry. But it seems to always override the java version. If you'd like to have a try, check https://maven.apache.org/shared/maven-archiver/examples/manifestFile.html

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

I don't think changing maven.cmd can make this script aware of the project peculiarities.

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

If the point is to avoid changing JAVA_HOME manually before some of your builds, maybe all you need is to make a wrapper batch file for maven referencing your jdk1.7:

如果要避免在某些构建之前手动更改JAVA_HOME,可能只需要为maven引用jdk1.7创建一个包装批处理文件:

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

Save this batch file as mvn_j7.bat inside your [MAVEN_HOME]\bin folder.Then you can run it anywhere, just like in the example below:

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

mvn_j7 clean package