"The command line is too long" -- when running maven test - java

Running $mvn test on a 64-bit Windows gives me the following error, even if I do $mvn test -Dgwt.genParam=false:
The command line is too long

Make sure you are using version 2.16 and that you have the useManifestOnlyJar option (as documented here).
For example:
<project>
[...]
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<useManifestOnlyJar>true</useManifestOnlyJar>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
[...]
</project>
This will create jar with a manifest that re-creates your classpath (as opposed to setting it via the CLASSPATH variable which is an approach that is affected by Windows' command-line limit problem).

Related

Type Could not initialize class org.apache.maven.plugin.war.util.WebappStructureSerial [duplicate]

When I am creating a Maven project I am getting this error in pom.xml
Could not initialize class
org.apache.maven.plugin.war.util.WebappStructureSerializer
-Maven Configuration Problem
web.xml is missing and <failOnMissingWebXml> is set to true.
It might because the version of maven-war-plugin is too old in your project.
Try to add below code in your pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
</project>
Note: you should have the build tag
place everything inside build tag then, it will not throw error
If you are getting this error within Eclipse (my Eclipse version was Version: 2021-09 (4.21.0), Build id: 20210910-1417 at the time of writing) and you recently tried to move to JDK 17, it appears Eclipse is not compatible with JDK 17. It requires at least JDK 11. I reverted back to Java 11.0.12 and the error has gone away.
I'm using Windows 10 and Eclipse Version: 2022-03 (4.23.0) Build id: 20220310-1457
My project uses JDK8, but newer versions of Eclipse require at least JDK11.
I configure this using Window->Preferences->Java->Installed JREs
as shown in the screenshot:
Maven from windows cmd shell works fine: mvn clean package
But, Eclipse gives an error on my pom.xml: Could not initialize class org.apache.maven.plugins.war.util.WebappStructureSerializer
For reference, Eclipse gave some other error messages while I was attempting to fix the issue, such as this one: Unable to make field private final java.util.Comparator java.util.TreeMap.comparator accessible: module java.base does not "opens java.util" to unnamed module
This error seems to be some compatibility issue with maven-war-plugin and the JDK17 that eclipse runs under. It's probably due to the 'module' permissions.
Using these helpful answers, I was able to find a workaround:
#Capricorn1 https://stackoverflow.com/a/69544567/4505142
#SreedharGS https://stackoverflow.com/a/29383652/4505142
I downloaded OpenJDK11 from: https://adoptium.net/
Updated eclipse.ini to use JDK11 instead of JDK17:
--launcher.appendVmargs
-vm
C:\Users\User1\OpenJDK11\jdk-11.0.15+10\bin
-vmargs
-Dosgi.requiredJavaVersion=11
-Dosgi.instance.area.default=#user.home/eclipse-workspace
-Dsun.java.command=Eclipse
-XX:+UseG1GC
-XX:+UseStringDeduplication
--add-modules=ALL-SYSTEM
-Dosgi.requiredJavaVersion=11
-Dosgi.dataAreaRequiresExplicitInit=true
-Dorg.eclipse.swt.graphics.Resource.reportNonDisposed=true
-Xms512m
-Xmx1024m
--add-modules=ALL-SYSTEM
For reference, here's the original eclipse.ini JDK17 path:
-vm
plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_17.0.2.v20220201-1208/jre/bin
Using JDK11 to run Eclipse, eliminated the pom.xml error "Could not initialize class org.apache.maven.plugins.war.util.WebappStructureSerializer"
For reference, here are the relevant snippets of my pom.xml:
<properties>
<spring-boot.version>1.5.13.RELEASE</spring-boot.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
<maven-war-plugin.version>3.0.0</maven-war-plugin.version>
</properties>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<packagingExcludes>**/application-*.properties</packagingExcludes>
</configuration>
<executions>
<execution>
<id>default-war</id>
<phase>prepare-package</phase>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
In Pom.xml, need to add the below snippet, and please find image attachment
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
After update Maven Project
enter image description here
Please use the below code snip:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</pluginManagement>
Please refer this
Try to add the plugin to your maven project, by adding this in your pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
After that: Right click -> Maven -> Update Project
I have the same problem as this when I create a maven project. So I got a solution for this just adding this plug into your pom.xml:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
If you already have it, then you should upgrade its version.

Running Testng.xml from POM.xml is not running tests in sequence

I am running testng.xml file using POM.xml by adding compiler and surefire plugins. It runs test but the sequence of tests is not as expected.
I have 10 classes mentioned in testng.xml and it runs in that sequence when i run through testng.xml. But when running through POM.xml the sequence goes like; first it runs all the 0 priority tests mentioned in all classes, then 1 priority tests and so on. It should run tests according to the classes sequence mentioned in testng.xml.
Any quick help will be much appreciated.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${jdk.level}</source>
<target>${jdk.level}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
under testng dependency the scope tag needed to be changed to compile from test and it resolved the issue as it needed to compile the build after adding plugins.
It resolved the issue.

Properly pass JDK argument into Maven pom file

Because I need to customize Host header in HTTP request, I need to start my Spring Boot Java app with following argument (available since JDK 12):
java -jar -Djdk.httpclient.allowRestrictedHeaders=host application.jar
but how to pass it into maven pom.xml file to be able to use this argument durring tests which are failing because of missing this flag?
I tried to use maven-compiler-plugin in following way:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-Djdk.httpclient.allowRestrictedHeaders=host</arg>
</compilerArgs>
</configuration>
</plugin>
but it's wrong:
error: invalid flag: -Djdk.httpclient.allowRestrictedHeaders=host
Following examples are not working either:
-jdk.httpclient.allowRestrictedHeaders=host
jdk.httpclient.allowRestrictedHeaders=host
So i tried even with spring-boot-maven-plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Djdk.httpclient.allowRestrictedHeaders=host</jvmArguments>
</configuration>
</plugin>
but it's also not working because in that case this flag is ignored and I got restriction error when I run mvn test. Which is not happening when I run java with this flag.
You seem to be configuring the wrong plugin. You said you need to "be able to use this argument during tests" which means you should be configuring Maven Surefire Plugin.
Have a look at the example they have provided. May be you can use systemProperties:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<systemProperties>
<property>
<name>propertyName</name>
<value>propertyValue</value>
</property>
[...]
</systemProperties>
</configuration>
</plugin>
or the argLine approach:
<argLine>-Djava.endorsed.dirs=...</argLine>

Travis Java build using different SDK for compilation and tests

I have a library that uses Java 8 classes if they are available and for older JRE versions provides a fallback implementation. It means I have to compile using Java 8 (or higher) but I want to execute the tests with JDK 7 to test the fallback. I can not figure out how to do it in Travis.
The easiest way is to create special Maven profile for Travis in pom.xml
<profile>
<id>travis</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable>/usr/lib/jvm/java-8-oracle/bin/javac</executable>
</configuration>
</plugin>
</plugins>
</build>
</profile>
And then activate it in .travis.yml
script: mvn install -P travis

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?
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
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).
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.
Then you have to add new Profiles in maven settings.xml file
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
Now you have to change Pom.xml in this way
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%
<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
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.

Categories