How to run test from different folder? - java

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>

Related

How should I edit my pom.xml file to import my maven project to sonarqube?

I am using JDK 1.8 and Maven using Eclipse IDE. I want to analyze my project on SonarQube 6.6. Here is my pom.xml file:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ADDITION</groupId>
<artifactId>Addition</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId> <!-- NOT org.junit here -->
<artifactId>junit-dep</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.sonarsource.scanner.maven/sonar-maven-plugin -->
<dependency>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And this is the output with error:
(localhost:9000 is the default port for SonarQube and it is working fine. But the problem is I cannot import my maven project to sonar. If anyone finds the solution, please help.
Thank you.)
C:\eclipse-workspace\Addition>mvn sonar:sonar
[INFO] Scanning for projects...
[WARNING] The artifact org.codehaus.mojo:sonar-maven-plugin:jar:3.9.1.2184 has been relocated to org.sonarsource.scanner.maven:sonar-maven-plugin:jar:3.9.1.2184: SonarQube plugin was moved to SonarSource organisation
[INFO]
[INFO] -----------------< ADDITION:Addition >-----------------
[INFO] Building Addition 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- sonar-maven-plugin:3.9.1.2184:sonar (default-cli) # Addition ---
[INFO] User cache: C:\Users\arghy\.sonar\cache
[ERROR] SonarQube server [http://localhost:9000] can not be reached
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.057 s
[INFO] Finished at: 2022-04-01T13:58:31+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar (default-cli) on project Addition: Unable to execute SonarScanner analysis: Fail to get bootstrap index from server: Failed to connect to localhost/0:0:0:0:0:0:0:1:9000: Connection refused: connect -> [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/MojoExecutionException
Looks like localhost resolves to the ipv6 adress, maybe sonarqube doesn't support that. What happens if you change http://localhost:9000 to http://127.0.0.1:9000?

Maven build fails with Java 8 and JUnit 5

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

maven build with my local-module artifactory (library)

I created a project named app-service that uses a core module(app-core). I include this core module in my project as maven dependency through <systemPath> that reside in project base directory.
<dependency>
<groupId>app-group</groupId>
<artifactId>app-core</artifactId>
<version>${project.version}</version>
<scope>system</scope>
<systemPath>${project.basedir}/app-core-0.1.1-SNAPSHOT.jar</systemPath>
</dependency>
I configure maven and run goal
mvn clean package install
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 44.733 s
[INFO] Finished at: 2017-04-05T12:27:46+05:30
[INFO] Final Memory: 29M/533M
[INFO] ------------------------------------------------------------------------
After BUILD SUCCESS $CLASSPATH does not contain the app-core.jar file, expect this it include all dependencies that listed in maven <dependencies>
I want to use this app-core module as compile scope. When I try this it prompt error
[ERROR] 'dependencies.dependency.systemPath' for app-group:app-core:jar must be omitted. This field may only be specified for a dependency with system scope. # line 71, column 25
Note: I do not upload this app-core on public repository due to security purpose. I want to use through project base directory because I need to deploy the same on Heroku.
Update
I googled it and found a plugin that installs local repository.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-external</id>
<phase>clean</phase>
<configuration>
<file>${project.basedir}/app-core-0.1.1-SNAPSHOT.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>app-group</groupId>
<artifactId>app-core</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
But the result is same......
You should follow this guide to Adding Unmanaged Dependencies to a Maven Project on Heroku. To summarize the guide, run:
mvn deploy:deploy-file -Durl=file:///path/to/app-group/app-core/ -Dfile=app-core-1.0.jar -DgroupId=app-group -DartifactId=app-core -Dpackaging=jar -Dversion=1.0
Then add this repository to your pom.xml:
<repository>
<id>project.local</id>
<name>project</name>
<url>file:${project.basedir}/repo</url>
</repository>
Then use the dependency i your pom.xml:
<dependency>
<groupId>app-group</groupId>
<artifactId>app-core</artifactId>
<version>1.0</version>
</dependency>
I think the error is telling you to delete part how you setup your dependency.
Try omitting parts of it such as:
<systemPath>${project.basedir}</systemPath>
..and working from there.
Edit:
I would also try the most limited amount of information.
<dependency>
<groupId>app-group</groupId>
<artifactId>app-core</artifactId>
<version>${project.version}</version>
</dependency>
Try changing the project version even. I've had problems with maven dependencies not pulling from repositories because the pom.xml was written incorrectly. The way I ended up solving them was to keep trying different ways of writing the dependency until it actually pulled it correctly.

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile

Given the below 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myApp</groupId>
<artifactId>malloc</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<repositories>
<repository>
<id>project</id>
<url>file:///${basedir}/lib</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.myApp</groupId>
<artifactId>myApp.core</artifactId>
<version>1.0.0</version>
</dependency>
... //other dependencies
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<webResources>
<webResource>
<directory>${project.build.directory}/WebContent/WEB-INF</directory>
<includes>
<include>web.xml</include>
</includes>
<targetPath>WEB-INF</targetPath>
<filtering>true</filtering>
</webResource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/WebContent/WEB-INF/lib</outputDirectory>
<includeScope>runtime</includeScope>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
And the following project structure:
I expect the maven-dependency-plugin would copy all the dependencies to the WebContent/WEB-INF/lib but when I run
mvn clean install
The following error is generated:
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building api-malloc 0.0.1
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for com.oracle:ojdbc6:jar:10.2.0.4.0 is missing, no dependency information available
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) # api-malloc ---
[INFO] Deleting C:\Development\malloc\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # api-malloc ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # api-malloc ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 8 source files to C:\Development\malloc\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Development/malloc/src/com/myApp/api/malloc/dto/RegionTypeDTO.java:[3,34] package com.myApp.api.core.dto does not exist
[ERROR] /C:/Development/malloc/src/com/myApp/api/malloc/dto/RegionTypeDTO.java:[5,39] cannot find symbol
symbol: class IDTO
[INFO] 26 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.209 s
[INFO] Finished at: 2015-11-26T15:33:40-05:00
[INFO] Final Memory: 21M/227M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project malloc: Compilation failure: Compilation failure:
[ERROR] /C:/Development/malloc/src/com/myApp/api/malloc/dto/RegionTypeDTO.java:[3,34] package com.myApp.api.core.dto does not exist
[ERROR] /C:/Development/malloc/src/com/myApp/api/malloc/dto/RegionTypeDTO.java:[5,39] cannot find symbol
[ERROR] symbol: class IDTO
[ERROR] location: class com.myApp.api.malloc.controllers.TestController
[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
I noticed the WEB-INF/lib folder is always empty, maven dependencies are not copied over.
I am suspecting the copy-dependencies plugin didn't get run before the compile stage but I can't really figure out - anything that I missed?
The copy-dependencies goal indeed will never be executed if the compilation fails because its binding is to the package phase, which comes after the compile phase (to which by default is linked the Maven Compiler Plugin).
If you want it to run before the compile phase, you need to change the phase value of the execution of the copy-dependencies goal. Changing to process-resources should be fine and should also make sense.
<phase>package</phase>
For a full description of phases, you can check official documentation, here.
You should also fix the compilation errors the build output is pointing at. I see the sourceDirectory element is overriding what Maven uses by default for Java source code (src\main\java), hence I suppose your code is directly under the src folder.
Updated: the package X does not exist error occurs when the code is referring to a package not resolved by the Java compiler, hence it is not able to see that package in the classpath, which means in the declared dependencies: does myApp.core contain that package and class? If yes, then the repositories element (the lib folder) is probably not properly providing the myApp dependency.
You can try to install the dependency locally in your .m2 maven cache using the Maven Install Plugin as specified here.
You could execute from the command line as following:
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file
-Dfile=lib\myApp.core-1.0.0.jar \
-DgroupId=com.myApp \
-DartifactId=myApp.core \
-Dversion=1.0.0 \
-Dpackaging=jar \
Side note: Maven prefers lower case groupid and artifactid tokens. Moreover, instead of using Camel Case (i.e. myApp), Maven convention is also use a dash for separating tokens (i.e. myapp-core).

Run Maven goal from Git Bash fail

I configured my pom.xml for executing TestNG suite file.
Executed file is passed as to Maven as parameter.
But trouble is that when I run from console:
mvn integration-test -Dmain-suite=${circuit-suite}
It works fine, test execution is started.
But when I tried to run it from Git Bash I got Error:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.892 s
[INFO] Finished at: 2015-04-14T11:30:20+02:00
[INFO] Final Memory: 31M/363M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18:test (default-test) on project webClientI
MPAutomation: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.18:test failed: There was an e
rror in the forked process
[ERROR] org.apache.maven.surefire.testset.TestSetFailedException: Suite file d:\HOME\IdeaProjects\webclient_suite\suite is not a valid file
[ERROR] at org.apache.maven.surefire.testng.TestNGXmlTestSuite.locateTestSets(TestNGXmlTestSuite.java:116)
[ERROR] at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:83)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
[ERROR] -> [Help 1]
I couldn't investigate why it searches for suite as suite file.
Here is pom snippet:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<smoke-suite>${basedir}/src/test/resources/webClientIMPSuites/SmokeSuiteCI.xml</smoke-suite>
<circuit-suite>${basedir}/src/test/resources/webClientIMPSuites/CircuitUITests.xml</circuit-suite>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18</version>
<executions>
<execution>
<id>default-test</id>
<phase>integration-test</phase>
<configuration>
<suiteXmlFiles>
<suiteXmlFiles>${main-suite}</suiteXmlFiles>
</suiteXmlFiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I passing testng suite file as parameter to Maven. Executable suite files are saved as properties for pom. At this case its value is:
${basedir}/src/test/resources/webClientIMPSuites/CircuitUITests.xml
Why this strange behavier happens?
How to solve it. And make execution at Git Bash too.
You probably have a shell/command line variable circuit-suite which expands to d:\HOME\IdeaProjects\webclient_suite\suite. Try
mvn integration-test -Dmain-suite=\${circuit-suite}
Note the backslash before the $. This tells BASH to leave this variable alone and just pass it to Maven.

Categories