Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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

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

At first my pom.xml was 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 is <source> and <target> tags but this tags does not works for java 1.7,1.8, maybe it worked for earlier versions.

So I had to make some changes in Mavens "settings.xml" and in "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>

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.

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.

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

here is the mvn.cmd code snippet

@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?

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

like image 564
mariami Avatar asked Oct 17 '22 23:10

mariami


1 Answers

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

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

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:

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:

mvn_j7 clean package
like image 152
Rafael Odon Avatar answered Nov 01 '22 13:11

Rafael Odon