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

Java 如何修改mvn.cmd以在MANIFEST.MF文件中获得正确的jdk构建版本,java,xml,maven,pom.xml,Java,Xml,Maven,Pom.xml,我已经在我的机器上安装了2个版本的java,1.7和1.8。 为了构建java项目,我使用maven 3.5.0 有些情况下,我必须使用java 1.7构建java项目, 因此,我将我的%JAVA\u HOME%环境变量从“C:\Program Files\JAVA\jdk1.8.0\u 131”更改为“C:\Program Files\JAVA\jdk1.7.0\u 80” 然后我想,如果我能做到这一点,pom.xml将决定java的版本,项目应该通过它来构建 起初,我的pom.xml是这样的

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

有些情况下,我必须使用java 1.7构建java项目, 因此,我将我的
%JAVA\u HOME%
环境变量从
“C:\Program Files\JAVA\jdk1.8.0\u 131”
更改为
“C:\Program Files\JAVA\jdk1.7.0\u 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>

现在有一个挑战

如何告诉
mvn.cmd
它是由用pom.xml编写的Java7构建的


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

我刚刚尝试使用maven archiver插件强制定制MANIFEST.MF并以某种方式确定“构建jdk”条目。但它似乎总是覆盖java版本。如果你想试一试,请查收

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

如果重点是避免在某些构建之前手动更改JAVA_主页,那么您可能只需要为maven创建一个包装批处理文件 参考您的jdk1.7:

SET "JAVA_HOME=C:\Program Files\Java\jdk1.7.0_80"
mvn.cmd %*
将此批处理文件另存为
mvn\u j7.bat
,保存在
[MAVEN\u HOME]\bin
文件夹中。然后您可以在任何地方运行它,如下例所示:

mvn_j7 clean package

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

首先,需要从环境路径中删除Java链接
C:\ProgramFiles\Java\jdk1.7.0\u 80\bin
%Java\u home%\bin

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

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

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

感谢您的建议
<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>
<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>