I have a Java project that uses Java 8. I want to use JUnit 5. I'm using Eclispe. If I add the JUnit 5 library in the project's Build Path, the build is successful inside Eclipse. However, when I build with Maven at command line it fails.
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: C:\Program Files\apache-maven-3.6.3\bin\..
Java version: 1.8.0_241, vendor: Oracle Corporation, runtime: C:\Program
Files\Java\jdk1.8.0_241\jre
Below the pom.xml. I've tried other versions of pom found in Google search, but all gave me the same errors.
UPDATE:
If I remove <scope>test</scope>from pom.xml, the build still fails.
<properties>
<!-- Dependency versions -->
<junit.jupiter.version>5.6.0</junit.jupiter.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<!-- Java 8 -->
<java.version>1.8</java.version>
<!-- Encoding -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<!-- Jupiter API for writing tests -->
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Maven Surefire plugin to run tests -->
<build>
<plugins>
<!-- plugin to run test cases from maven -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
<!-- Maven plugin to use perticular java version to compile code -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
[INFO] 5 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.473 s
[INFO] Finished at: 2020-04-28T12:19:11-04:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project Java8: Compilation failure: Compilation failure:
[ERROR] C:\Users\Documents\Java8\src\main\java\com\name\stream\ClassName.java:[8,23] error: package org.junit does not exist
[ERROR] C:\Users\Documents\Java8\src\main\java\com\name\stream\ClassName.java:[8] error: static import only from classes and interfaces
[ERROR] C:\Users\Documents\Java8\src\main\java\com\name\stream\ClassName.java:[31,2] error: cannot find symbol
[ERROR] symbol: method assertEquals(Optional<String>,Optional<Object>)
[ERROR] location: class ClassName
[ERROR] C:\Users\Documents\Java8\src\main\java\com\name\stream\ClassName.java:[33,2] error: cannot find symbol
[ERROR] symbol: method assertEquals(Optional<Optional<String>>,Optional<Object>)
[ERROR] location: class ClassName
[ERROR] C:\Users\Documents\Java8\src\main\java\com\name\stream\ClassName.java:[44,2] error: cannot find symbol
[ERROR] symbol: method assertEquals(Optional<String>,Optional<Object>)
[ERROR] location: class ClassName
[ERROR] -> [Help 1]
Eclipse does not correctly separate test scope and compile scope dependencies so keep the scope test which is correct. You have to create a Test class in
src/test/java/com/name/stream/ClassNameTest.java and use the appropriate annotations which needs to be from org.junit.jupiter.api package
Take a look at this example project: github.com/khmarbaise/basic-junit-jupiter-test (Simple Test case and assertions also simple class with a single method just to show how it works..)...It builds on command line, in IDEA Intellij and should also work in Eclipse and works on GitHub Actions
Related
This is quite unexpected, I recently switched to a new machine and while setting up my dev environment, one of my services has been giving this error on running mvn clean install
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:35 min
[INFO] Finished at: 2020-11-21T22:27:09+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.coderplus.maven.plugins:copy-rename-maven-plugin:1.0:copy (copy-file) on project my-service: Execution copy-file of
goal com.coderplus.maven.plugins:copy-rename-maven-plugin:1.0:copy failed: A required class was missing while executing com.coderplus.maven.plugins:copy-renam
e-maven-plugin:1.0:copy: org/codehaus/plexus/util/Scanner
[ERROR] -----------------------------------------------------
[ERROR] realm = plugin>com.coderplus.maven.plugins:copy-rename-maven-plugin:1.0
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/C:/Users/userName/.m2/repository/com/coderplus/maven/plugins/copy-rename-maven-plugin/1.0/copy-rename-maven-plugin-1.0.jar
[ERROR] urls[1] = file:/C:/Users/userName/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
[ERROR] urls[2] = file:/C:/Users/userName/.m2/repository/org/apache/maven/reporting/maven-reporting-api/2.0.9/maven-reporting-api-2.0.9.jar
[ERROR] urls[3] = file:/C:/Users/userName/.m2/repository/org/apache/maven/doxia/doxia-sink-api/1.0-alpha-10/doxia-sink-api-1.0-alpha-10.jar
[ERROR] urls[4] = file:/C:/Users/userName/.m2/repository/commons-cli/commons-cli/1.0/commons-cli-1.0.jar
[ERROR] urls[5] = file:/C:/Users/userName/.m2/repository/org/codehaus/plexus/plexus-interactivity-api/1.0-alpha-4/plexus-interactivity-api-1.0-alpha-4.jar
[ERROR] urls[6] = file:/C:/Users/userName/.m2/repository/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar
[ERROR] urls[7] = file:/C:/Users/userName/.m2/repository/org/sonatype/plexus/plexus-build-api/0.0.7/plexus-build-api-0.0.7.jar
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import from realm ClassRealm[maven.api, parent: null]]
[ERROR]
[ERROR] -----------------------------------------------------
[ERROR] : org.codehaus.plexus.util.Scanner
My first guess would be that I have a dependency missing in my project(not sure which one), although it does BUILD SUCCESS on existing machines including the Jenkins instance.
None of the other services I run have an issue like this.
I've verified that the jars mentioned are not corrupt. Tried deleting the whole .m2 directory and starting again as well.
Are there any other possibilities I might be missing here?
Edit: Adding the <plugins> section of my pom.xml
I've been recommended using a newer plugin, but this may add help narrow down the issue further:
<plugins>
<!-- Docker Build Plugin START -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-filtering</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
<!-- Docker Build Plugin END -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration><addResources>true</addResources></configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
I've removed the <execution> part from these to save some scrolling.
You could use the much newer Wagon Maven Plugin:
Use this plugin to view and transfer resources between repositories using Maven Wagon.
with its copy goal:
Copy artifacts from one Wagon repository to another Wagon repository.
instead of the old copy-rename-maven-plugin.
Don't get confused by Wagon repository. There are simply source/target URLs and fromDir/toDir directories. Wagon supports the file: protocol.
Re: one of your comments to your question: The maven-resources-plugin:2.7 has in its POM:
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.15</version>
</dependency>
and no copy-rename-maven-plugin, of course.
I am currently doing a software engineering module where we learn about CI and TDD. As an assignment we must create a Java project using maven, JUnit, and Shippable. My files include three java classes (Student, Course, and Module classes) and a simple test for each that tests getters and setters.
I am using:
Apache NetBeans 12.0
Maven (default is 3.6.3 but I added a plugin for 3.8)
Openjdk11
My pom file:
<?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>com.nuig</groupId>
<artifactId>assignment1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<name>assignment1</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release> <!--or <release>10</release>-->
</configuration>
</plugin>
</plugins>
</build>
</project>
And shippable.yml file:
language: java
jdk:
- openjdk11
after_success:
- mvn clean verify
- mvn clean cobertura:cobertura
- mvn test
When I build the code in netbeans, it builds and passes the tests fine. But once I push and go to shippable console, it keeps failing at mvn clean cobertura:cobertura with the following error:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.165 s
[INFO] Finished at: 2020-10-11T00:26:35Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:cobertura-maven-plugin:2.7:instrument (default-cli) on project assignment1: Execution default-cli of goal org.codehaus.mojo:cobertura-maven-plugin:2.7:instrument failed: Plugin org.codehaus.mojo:cobertura-maven-plugin:2.7 or one of its dependencies could not be resolved: Could not find artifact com.sun:tools:jar:0 at specified path /usr/lib/jvm/java-11-openjdk-amd64/../lib/tools.jar -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException
I am really unfamiliar with all these technologies and I have already tried several suggestions from answers to similar questions but to no vail.
Would anyone be able to help with this issue? Much appreciated.
I have project
my-tests
src
test
java
my
demo
srv
src
main
java
my
demo
I want to run mvn clean install on srv and then to run the tests from my-tests
I tried to add plugin to the pom.xml of srv
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>add-test-source</id>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>../my-tests</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
I got an error of compile
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.592 s
[INFO] Finished at: 2020-04-27T16:35:14+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project sapinttest-srv: Compilation failure: Compilation failure:
[ERROR] /C:/Users/Downloads/app/integration-tests/src/test/java/my/company/HelloWorldServletTest.java:[3,22] package io.restassured does not exist
[ERROR] /C:/Users/Downloads/app/integration-tests/src/test/java/my/company/HelloWorldServletTest.java:[4,47] package org.jboss.arquillian.container.test.api does not exist
[ERROR] /C:/Users/Downloads/app/integration-tests/src/test/java/my/demo/HelloWorldServletTest.java:[5,34] package org.jboss.arquillian.junit does not exist
[ERROR] /C:/Users/Downloads/app/integration-tests/src/test/java/my/demo/HelloWorldServletTest.java:[6,37] package org.jboss.arquillian.test.api does not exist
[ERROR] /C:/Users/Downloads/app/integration-tests/src/test/java/my/demo/HelloWorldServletTest.java:[7,37] package org.jboss.shrinkwrap.api.spec does not exist
[ERROR] /C:/Users//Downloads/app/integration-tests/src/test/java/my/demo/HelloWorldServletTest.java:[8,17] package org.junit does not exist
when I tried to run test from the my-tests folder the compile was ok but the tests didn't run
mvn clean install -DskipTests=false
Could you advise me please ?
Fixing compilation errors
The error message states that some third library packages could not be found.
Being my-tests a Maven module, you should add to its pom.xml the dependencies that contains these packages.
pom.xml
<project>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- TODO: Add other dependencies here. -->
</dependencies>
...
</project>
To find which dependency contains the package, you could search on Maven Central using the following criteria:
fc:package-name
e.g.:
fc:org.jboss.arquillian.junit
Direct link: https://search.maven.org/search?q=fc:org.jboss.arquillian.junit
Changing the test folder
Being your goal simply running tests on a folder different than src/test/java, you should change the testSourceDirectory property.
pom.xml
<project>
<build>
<testSourceDirectory>${project.basedir}/src/custom-test-folder/java</testSourceDirectory>
</build>
...
</project>
As I am not expert for maven, I'm a bit lost here. We have projects which fail with the following error
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-site-plugin:3.7:site (default-cli) on project xxxx: Execution default-cli of goal org.apache.maven.plugins:maven-site-plugin:3.7:site failed: Invalid byte tag in constant pool: 18 -> [Help 1]
when executing this command
mvn site:site site-deploy -e -U
This occurs since we switched to java 8 and to my understanding it is somehow related to Java 8 and BCEL. However the message in the bug report is not exactly the same so I am not sure how or what do I have to configure to overcome the problem.
Here the essential info on how we have configured the site-plugin in the parent pom:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.6</version>
<configuration>
<locales>en</locales>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-webdav-jackrabbit</artifactId>
<version>${wagon.webdav.version}</version>
</dependency>
</dependencies>
Actually a rather stupid error. The issue was with the used clirr which was 2.3
08:26:16 [INFO] <<< clirr-maven-plugin:2.3:clirr < compile # xxxxx <<<
Actually clirr was configured in the parent pom to use 2.8
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>clirr-maven-plugin</artifactId>
<version>2.8</version>
</plugin>
However, in the local maven repository there was still an outdated parent pom which refered to 2.3 instead 2.8. Deleting the parent pom from the local repo forced the re-download of the parent pom and now we are using the correct configuration
Used Bitnami Tomcat Stack to create a instance in EC2 . Everything works perfect in matter of few mins. But at final stage to deploy application maven is failing to compile the source code. Eventhoug JAVA_HOME is perfectly set it is failing to compile with below error log.
root#ip-172-31-18-89:/mnt/apps/stutzen-backend# echo $JAVA_HOME
/opt/bitnami/java
root#ip-172-31-18-89:/mnt/webapps/stutzen-backend# mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building ware27 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) # ware27 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) # ware27 ---
[INFO] Compiling 34 source files to /mnt/webapps/stutzen-backend/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] Unable to locate the Javac Compiler in:
/opt/bitnami/java/../lib/tools.jar
Please ensure you are using JDK 1.4 or above and
not a JRE (the com.sun.tools.javac.Main class is required).
In most cases you can change the location of your Java
installation by setting the JAVA_HOME environment variable.
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.245s
[INFO] Finished at: Sun Jul 13 10:37:54 UTC 2014
[INFO] Final Memory: 6M/25M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project ware27: Compilation failure
[ERROR] Unable to locate the Javac Compiler in:
[ERROR] /opt/bitnami/java/../lib/tools.jar
[ERROR] Please ensure you are using JDK 1.4 or above and
[ERROR] not a JRE (the com.sun.tools.javac.Main class is required).
[ERROR] In most cases you can change the location of your Java
[ERROR] installation by setting the JAVA_HOME environment variable.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
How to deal with it ?
The issue is that the maven compiler plugin is configured by default to use the a Java 1.5 compiler (see the source and target options in the official documentation) while Bitnami Tomcat Stack is includes Java 1.7. You will need to configure your projects to use Java 1.7 instead. For this you can define a different value for the maven.compiler.source and maven.compiler.target properties or define the maven.compiler.compilerVersion instead which will work as well. In both cases you need to set maven.compiler.fork to true. The example below shows how to configure this per project:
pom.xml:
<project>
(...)
<properties>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
<maven.compiler.fork>true</maven.compiler.fork>
</properties>
(...)
</project>
However you will likely want to define these settings as global settings for all your projects. In that case you can edit the settings.xml in ($M2_HOME/conf/settings.xml) and define a profile which will be always actived:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" ...>
(...)
<profiles>
<profile>
<id>env-dev</id>
<properties>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
<maven.compiler.fork>true</maven.compiler.fork>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>env-dev</activeProfile>
</activeProfiles>
</settings>
kaysa's answer is correct. The solution, however, can be achieved a little easier by adding a maven-compiler-plugin to your pom as shown below:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
I'm not sure if this helps you or not. add a profile to your pom.xml :)
<profiles>
<profile>
<id>default-tools.jar</id>
<activation>
<property>
<name>java.vendor</name>
<value>Oracle Corporation</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.4.2</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
</profiles>
give your true tools.jar address and run maven with this profile
if it's not working forget that and change your java home to something like : /opt/bitnami/jdk1.7.0_51/jre:)