I have a simple java project with on Junit test case that has the current architecture:
pom.xml
src/main/java/com/Example.Java
src/test/java/com/ExampleTest.java
The contents of pom.xml are as follow:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>SampleExample</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
To execute the tests I simply call mvn test from bash. This as expected will run the test. Now to my question:
specifying javagent outside of maven is simply done by specificity the -javaagent option. How do I do this within the maven framework such that when I execute mvn test the agent I specify is loaded? (i.e how do I add custom arguments that maven will pass to the 'java' command when the tests are executed)
Define the Surefire plugin in your POM and pass the JVM arg via Surefire's configuration.
For example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<argLine>-javaagent:/path/to/javaagent</argLine>
</configuration>
</plugin>
For background: the test goal is bound to surefire:test by default (more details on default bindings here) so you have been already been running the Surefire plugin perhaps without realising it. The only change now is that you need to change the configuration of the Surefire plugin and this requires you to include it explicitly in your POM as per the example I showed above. You cannot run the test goal without using the Surefire plugin and you cannot tell Surefire to use a JVM arg without configuring the Surefire plugin to do so.
Related
After using
mvn clean install
and then
java -jar executable.jar
I get this error:
java.lang.ClassNotFoundException: org.sqlite.JDBC
This is my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>****</groupId>
<artifactId>****</artifactId>
<version>0.7-SNAPSHOT</version>
<packaging>jar</packaging>
<name>****</name>
<description>****</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.23.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit/junit5-engine -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-engine</artifactId>
<version>5.0.0-ALPHA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.tinyjee.jgraphx/jgraphx -->
<dependency>
<groupId>org.tinyjee.jgraphx</groupId>
<artifactId>jgraphx</artifactId>
<version>3.4.1.3</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>******</sourceDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>*****</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>******</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Running the program inside intelliJ works without problems. I included it there from the project structure.
I replaced names and directories with ****. This is a school project and I don't want my prof accusing me of providing solutions to other groups in case they find this stackoverflow entry.
Probably you are getting this only when you are running your jar because the dependencies are not available/packaged inside of it.
Try generating a "fat jar" (also known as uber-jar), it will package all your dependencies inside the jar:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>YOUR_JAR_FINAL_NAME</finalName>
</configuration>
</plugin>
</plugins>
</build>
Documentation related to the maven-shade-plugin can be found in here
Since you are using a runnable jar file, you can follow this section of the documentation related to Executable Jars
Some background.
Maven install never installs dependencies;
it only installs the project that is build via the POM.
The installation of dependencies are a task that you must also perform
if you don't use either a "fat jar" (which I can't recommend) or use
the new spring boot executable jar implementation.
The classic "fat jar" is an amazingly terrible
(but often the only option)
solution for a problem like this.
Consider using Spring-Boot;
they have developed a new,
sane,
version of an executable jar file that includes the dependencies within the executable jar and adds them to the classpath at startup.
This functionality is similar to the functionality of a "war" file when it is added to
a JEE container.
Caveat: I don't work for Pivotal,
I just like much of their work (the Spring Framework).
Edit:
Here is a link to the
Executable Jar Section in the Spring Boot Reference.
It contains some details.
I am using maven jacoco plugin for code coverage. I decided to exclude some classes from jacoco coverage report. I found here, how to do it.
So my updated pom file looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>payment-rest</artifactId>
<packaging>war</packaging>
<name>payment-rest</name>
<parent>
<artifactId>payment-ws</artifactId>
<groupId>com.example.foo</groupId>
<version>1.0.0-INTEGRATION-SNAPSHOT</version>
</parent>
<properties>
<jacoco.line.coverage>0.78</jacoco.line.coverage>
<jacoco.branch.coverage>1.00</jacoco.branch.coverage>
<servlet.version>3.0.1</servlet.version>
</properties>
<dependencies>
<!-- all dependecies here-->
</dependencies>
<build>
<finalName>payment-ws</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
**/com/example/foo/payment/configuration/**.*
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<attachClasses>true</attachClasses>
<classesClassifier>classes</classesClassifier>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
So, when I am running mvn clean install command, I gets this error ():
Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test failed: The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
If I remove exclusion part, project builds successfully.
Can someone advise me how can I solve this issue?
issue is occur because maven-surefire-plugin is missing into your project pom.xml. you need to add this plugin into pom.xml, after adding need to update and rebuild the project and run it.
you can find the maven plugin below:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
you can refer higher version from 2.19.1.
I'm adding the jspc plugin like this in my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo.jspc</groupId>
<artifactId>jspc-maven-plugin</artifactId>
<version>2.0-alpha-3</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<inputwebxml>${basedir}/src/main/webapp/WEB-INF/web.xml</inputwebxml>
<sources>
<directory>${basedir}/src/main/webapp/jsp</directory>
<includes>
<include>**/*.jsp</include>
</includes>
</sources>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo.jspc</groupId>
<artifactId>jspc-compiler-tomcat6</artifactId>
<version>2.0-alpha-3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
But, when I run mvn clean dependency:copy-dependencies install, I don't see any classes are generated. From here, http://hasini-gunasinghe.blogspot.com/2011/09/how-to-use-pre-compiled-jsps-in-webapp.html, I'm supposed to see a target/jsp-source directory, but I don't have it.
Any problem with my pom.xml?
The problem is that you are declaring the plugin inside <pluginManagement> and it is not declared inside a <plugins>. Quoting Maven documentation (emphasis mine):
Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one
which is not the case here.
As such, you just need to remove the <pluginManagement> element and let the plugin be declared directly inside <plugins>.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo.jspc</groupId>
<artifactId>jspc-maven-plugin</artifactId>
<version>2.0-alpha-3</version>
<!-- rest of configuration -->
</plugin>
</plugins>
</buil>
I am trying to use checkstyles google_checks.xml with maven-checkstyle-plugin. If I use the google_checks.xml with the latest checkstyle intelliJ plugin everything is correct but when I try configurating it via maven-checkstyle plugin I get this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.13:check (default-cli) on project XX_XX_XX: Failed during checkstyle configuration: cannot initialize module TreeWalker - Unable to instantiate AvoidEscapedUnicodeCharacters:
Unable to instantiate AvoidEscapedUnicodeCharactersCheck
My pom.xml looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<properties>
[...]
<checkstyle.file.path>develop/checkstyle/google_checks.xml</checkstyle.file.path>
</properties>
[...]
<build>
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.13</version>
<configuration>
<configLocation>${checkstyle.file.path}</configLocation>
<failOnViolation>false</failOnViolation>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<configLocation>${checkstyle.file.path}</configLocation>
<failOnViolation>false</failOnViolation>
</configuration>
</plugin>
</plugins>
</reporting>
Do you guys have some suggestions about what could be wrong?
fixed this by updating the checkstyle-dependency manually to the latest stable version:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.13</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${checkstyle.latest.version}</version>
</dependency>
</dependencies>
<configuration>
<configLocation>${checkstyle.file.path}</configLocation>
<failOnViolation>false</failOnViolation>
</configuration>
</plugin>
Maven checkstyle plugin uses checkstyle 5.7 (the first line of plugin description).
Checkstyle 5.7 does not have this check (see checks package on grepcode).
You need either to disable this check or to wait for official fix of MCHECKSTYLE-261.
I give a demo at
https://github.com/favoorr/Maven-Checkstyle-Multimodule-Use
Multi modules and use Google Chechstyle
I'm using Maven's exec:java to run jline for one of my projects (current POM attached below). The project used to be a single component, so all dependencies were in the same POM as the exec:java plugin definition. This worked great and all the dependencies were picked up and put on the classpath when I ran 'mvn exec:java'. However, I've now split up the project into a few modules and would like the dependencies from each module to be picked up when exec:java is run, but I can't figure out how to configure it. Advice would be greatly appreciated!
thanks,
Nick
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>Lensfield</name>
<groupId>org.lensfield</groupId>
<artifactId>lensfield-pom</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includeProjectDependencies>true</includeProjectDependencies>
<includePluginDependencies>true</includePluginDependencies>
<executableDependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
</executableDependency>
<mainClass>jline.ConsoleRunner</mainClass>
<arguments>
<argument>clojure.lang.Repl</argument>
</arguments>
</configuration>
<dependencies>
<dependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
<version>0.9.94</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<modules>
<module>lensfield-share</module>
<module>lensfield-build</module>
<module>lensfield-webapp</module>
</modules>
</project>
You can specify a parent POM for the project and define the exec-plugin in the pluginManagement section of the parent. This means that the plugin configuration will be available to any child POM that declares a minimal plugin configuration. The following would be sufficient.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
</plugin>
When the child is processed it will inherit the configuration from the parent, and the exec-plugin will be executed with the current project's dependencies.