How to set the file.encoding property at exec-maven-plugin? - maven

I trying to exec my standalone application via exec-maven-plugin, but it started with WIN encoding, not UTF-8. I read about Java command line key -Dfile.encoding=UTF-8. How to set this property to my application?
Thanx.
maven pom:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<executable>java</executable>
<mainClass>my.main.Class</mainClass>
</configuration>
</plugin>

To set encoding for mvn exec:java, set MAVEN_OPTS environment variable, e.g.:
export MAVEN_OPTS=-Dfile.encoding=utf-8
Here is what exec-maven-plugin usage page says:
Note: The java goal doesn't spawn a new process. Any VM specific option that you want to pass to the executed class must be passed to the Maven VM using the MAVEN_OPTS environment variable. E.g.
MAVEN_OPTS=-Xmx1024m
Otherwise consider using the exec goal.

According to the exec-maven-plugin documentation, it should look like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>my.main.Class</mainClass>
<commandlineArgs>-Dfile.encoding=UTF-8</commandlineArgs>
</configuration>
</plugin>

more direct method than Todd's (his one's still cool though):
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>my.main.Class</mainClass>
<systemProperties>
<systemProperty>
<key>file.encoding</key>
<value>UTF-8</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
samples here.

Related

Passing vm arguments to exec-maven-plugin

Guys I have upgraded my project to Java 17 that's why I need these lines to VM to my program works without error.
=--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED
My project is working but I have another sub project which I use for testing purposes.It is run on JavaFX and it need previous arguments to work correctly.
I have tried adding as CommandLineArgs and arguments but still arguments are not passing through to vm and giving errors.
<profile>
<id>smoke</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy jars for tests</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeProjectDependencies>false</includeProjectDependencies>
<includePluginDependencies>false</includePluginDependencies>
<stripVersion>true</stripVersion>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<classpathScope>test</classpathScope>
<includeProjectDependencies>false</includeProjectDependencies>
<includePluginDependencies>false</includePluginDependencies>
<executable>${env.HOME}/java/bin/java.exe</executable>
<workingDirectory>${project.build.directory}</workingDirectory>
<arguments>
<argument>-javaagent:${env.HOME}/wildfly/standalone/lib/ext/agent.jar</argument>
<argument>-classpath</argument>
<argument>lib/*;classes</argument>
<argument>-DJBOSS_HOME=${JBOSS_HOME}</argument>
<argument>-DJAVA_HOME=${env.HOME}/java</argument>
<argument>-Duser.home=${env.USERPROFILE}</argument>
<argument>-Djava.library.path=${JBOSS_HOME}/bin</argument>
<argument>-DTESTING_FROM_COMMANDLINE=true</argument>
<mainClass>com.test.GenerateSmoke</mainClass>
<commandlineArgs>-Dexec.args=--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED</commandlineArgs>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>
If you have any idea please let me know.Thanks
I had a similar issue when running tests with the maven-surefire-plugin.
In my case I added it like this into the pom:
<configuration>
<argLine>${argLine} --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED</argLine>
</configuration>
Maybe it is the same in your case.
The plugins configuration options are documented here: https://www.mojohaus.org/exec-maven-plugin/exec-mojo.html
commandlineArgs or -Dexec.args are both an alternative option to override the entire executed command line you specify. These can not be used together with (other) arguments.
So you either define the entire java command line to execute as:
arguments in the pom, or
commandlineArgs in the pom, or
exec.args as an environment variable (so mvn exec:exec -Dexec.args=...)
You should be able to add the arguments to your current plugin configuration like so:
<configuration>
<arguments>
<argument>...</argument>
<argument>--add-opens</argument>
<argument>java.base/java.lang=ALL-UNNAMED</argument>
<argument>--add-opens</argument>
<argument>java.base/java.util=ALL-UNNAMED</argument>
<argument>...</argument>
</arguments>
</configuration>
Depending on what you run these arguments might need to be inserted in a specific place, so the order of these arguments matters.
That is where you can experiment with substituting the ... in my example with your current arguments.
As a test i've ran a java -jar with these options, and that works. I've enabled the maven argument -X for debug logging.
[DEBUG] Executing command line: [some\path\jdk-17.0.2\bin\java.exe, --add-opens, java.base/java.lang=ALL-UNNAMED, --add-opens, java.base/java.util=ALL-UNNAMED, -jar, my.jar]
And if i make an "accidental" typo in my add-opens we see that these arguments are indeed parsed by the java process:
[DEBUG] Executing command line: [some\path\jdk-17.0.2\bin\java.exe, --add-typo, java.base/java.lang=ALL-UNNAMED, --add-opens, java.base/java.util=ALL-UNNAMED, -jar, my.jar]
Unrecognized option: --add-typo
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

Running Serenity -Cucumber Test cases in parallel

I'm new to Serenity and BDD. I've a small demo project based on Serenity-Cucumber and Page Based model. Below is the structure of the project:
The Login and Logout features have around 8 scenarios.
I want to be able to run the feature files in parallel. What is the easiest and most effective way to achieve this?
So far I have
Created separate Runner class for each feature and then used failsafe or surefire plugin - This is something I don't want as I don't want a new runner for each feature file.
Used the "cucumber-vm-parallel-plugin". I copy pasted below code in my pom file. Nothing happened.
<plugin>
<groupId>com.github.temyers</groupId>
<artifactId>cucumber-jvm-parallel-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>generateRunners</id>
<phase>validate</phase>
<goals>
<goal>generateRunners</goal>
</goals>
<configuration>
<glue>com.automationrhapsody.cucumber.parallel.tests</glue>
<featuresDirectory>src/test/resources/com</featuresDirectory>
<cucumberOutputDir>target/cucumber-parallel</cucumberOutputDir>
<format>json,html</format>
<tags>"~#ignored"</tags>
</configuration>
</execution>
</executions>
Looked into Serenity documentation and ran my program using following parameters, but could not achieve parallel execution.
mvn verify -Dthucydides.batch.count=2 -Dthucydides.batch.number=2
I'm stuck over here. Any help (easy and effective) will be appreciated.
Also, please suggest how options 2 and 3 above can be done correctly
Thanks.
You also need to add below plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<forkCount>5</forkCount>
<reuseForks>true</reuseForks>
<includes>
<include>**/*IT.class</include>
</includes>
</configuration>
</plugin>

How to tell Maven to execute testng tests one by one each in new JVM instance?

Is it possible to tell Maven to execute every testng test in new JVM instance (fork) in serial mode, i.e. one by one.
The configuration below works for junit, but not works for testng
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<forkMode>always</forkMode>
</configuration>
Does anybody know how to set for testng?
#ben75, Thanks for your answer, But it doesn't work for me.
The key point is we use suiteXmlFiles to specify which case to run, and if we use suiteXmlFiles, the forkMode or (forkCount resuseForks) don't work.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<includes>
<include>**/Test*.java</include>
</includes>
<forkMode>always</forkMode>
</configuration>
</plugin>
This configuration works for me.
You can use this
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
...
It's a very expensive configuration (i.e. it will take a long time to run your tests... so it's better to not use it, but you probably have some (good ?) reasons to use it).
This solution is the way to go since version 2.14, so I suggest you to upgrade your surefire-plugin version.
the reference is here

Specify javaagent argument with Maven exec plugin

I have a similar question to: this previous question
I am converting a Java project using Netbeans to Maven. In order to launch the program, one of the command-line arguments we need is the -javaagent setting. e.g.
-javaagent:lib/eclipselink.jar
I'm trying to get Netbeans to launch the application for development use (we will write custom launch scripts for final deployment)
Since I'm using Maven to manage the Eclipselink dependencies, I may not know the exact filename of the Eclipselink jar file. It may be something like eclipselink-2.1.1.jar based on the version I have configured in the pom.xml file.
How do I configure the exec-maven-plugin to pass the exact eclipselink filename to the command line argument?
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Xmx1000m</argument>
<argument>-javaagent:lib/eclipselink.jar</argument> <==== HELP?
<argument>-classpath</argument>
<classpath/>
<argument>my.App</argument>
</arguments>
</configuration>
</plugin>
I figured out a way that seems to work well.
First, setup the maven-dependency-plugin to always run the "properties" goal.
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>getClasspathFilenames</id>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
Later on, use the property it sets as documented here with the form:
groupId:artifactId:type:[classifier]
e.g.
<argument>-javaagent:${mygroup:eclipselink:jar}</argument>
Simply define a property for the eclipse link version and use the property in your <dependency> and the exec plugin:
<properties>
<eclipselink.version>2.4.0</eclipselink.version>
</properties>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>${eclipselink.version}</version>
</dependency>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Xmx1000m</argument>
<argument>-javaagent:lib/eclipselink-${eclipselink.version}.jar</argument>
<argument>-classpath</argument>
<classpath/>
<argument>my.App</argument>
</arguments>
</configuration>
</plugin>
the maven-dependency-plugin and exec-maven-plugin should be put under the node ,otherwise it will not work

How to pass environment variable to process started as mvn jetty:run?

The process started by maven jetty plugin seems ignoring any environment variables I specify.
So far I've tried adding variable through command line like:
set myvariable=1
Also I've tried adding something like "-Dmyvariable=1" to MAVEN_OPTS variable.
Nothing helps.
Just to be clear, I need to pass variable not to maven but to the resulting process, i.e. directly to jetty server.
You need to specify a systemProperties section in the plugin's configuration:
<project>
...
<plugins>
...
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
...
<systemProperties>
<systemProperty>
<name>myvariable</name>
<value>1</value>
</systemProperty>
...
</systemProperties>
</configuration>
</plugin>
</plugins>
</project>

Resources