How to modify mvn.cmd to get the correct jdk assembly version in MANIFEST.MF file

I installed 2 versions of java on my machine, 1.7 and 1.8. to create my java projects i use maven 3.5.0.

There are some cases when I need to build a java project using java 1.7, so I change my environment variable %JAVA_HOME%to "C:\Program Files\Java\jdk1.7.0_80"from "C:\Program Files\Java\jdk1.8.0_131".

Then I thought that if I can do this, then pom.xml will determine the java version by which the project should be created.

At first, my pom.xml looked like this:

<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 are tags <source>and <target>, but these tags do not work for java 1.7.1.8, maybe they worked for earlier versions.

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

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

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

mvn.cmd, java 7, pom.xml?

mvn.cmd jdk MANIFEST.MF?

+4
2

, JDK MANIFEST.MF. ( ).

Java C:\Program Files\Java\jdk1.7.0_80\bin %java_home%\bin PATH.

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

, <java-version>, <java-1.8>, <jdk>, <wsimport>. mvn -Djava=7 mvn -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. , , , java- %PATH%

<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 java- java/wsimport , .

+2

maven-archiver, MANIFEST.MF - "Build-Jdk". , , java. , https://maven.apache.org/shared/maven-archiver/examples/manifestFile.html

, maven.cmd script .

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

Source: https://habr.com/ru/post/1693065/


All Articles