Running Maven Tests - java

I am learning Java, part of the courseware is Maven.
I have created a simple Maven project. And in the test class I have added the following code:
package maventest2;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
public class firstclass {
#Tag("FirstTests")
#DisplayName("This is the first group test I working on")
#Test
public void someTest() {
System.out.println("This in some test running");
}
}
When I run mvn test command I get the following:
Microsoft Windows [Version 10.0.18363.1256]
(c) 2019 Microsoft Corporation. All rights reserved.
C:\Users\pawan\eclipse-workspace\maventest2>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.taskone:maventest2 >-----------------------
[INFO] Building maventest2 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # maventest2 ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # maventest2 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # maventest2 ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) # maventest2 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M3:test (default-test) # maventest2 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.139 s
[INFO] Finished at: 2020-12-19T23:32:34-05:00
[INFO] ------------------------------------------------------------------------
Why is the test not being run? When I use the option to Run As ->JUnit Test. It works fine and I get the passed result.
Please advice.
pom 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>com.taskone</groupId>
<artifactId>maventest2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<includes>
<include>%regex[.*Test.*]</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId> <!-- NOT org.junit here -->
<artifactId>junit-dep</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Directory structure of project
The test file is in src/test/java

Two reasons:
Your test class name doesn't match the regex you have in your surefire configuration. You have explicitly said that the class name needs to match *Test*
You are importing the #Test annotation from org.junit, not org.junit.jupiter.api.Test

Related

Maven Build Success, but no result in target folder

For starters, I have not worked with Maven much at all.
I had everything working except for the version not being specified, but when I did specify the Maven version in the pom.xml, it still didn't work.
Note: Every run showed "Build Success" but none put a completed jar file in the target folder, which is what I thought should have happened.
Here is my output from eclipse:
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< TromboneSMP:VoluntaryPullFromEternity >----------------
[INFO] Building VoluntaryPullFromEternity 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # VoluntaryPullFromEternity ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) # VoluntaryPullFromEternity ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Users\Roboverse\eclipse-workspace\VoluntaryPullFromEternity\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # VoluntaryPullFromEternity ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:testCompile (default-testCompile) # VoluntaryPullFromEternity ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) # VoluntaryPullFromEternity ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) # VoluntaryPullFromEternity ---
[INFO] Building jar: C:\Users\Roboverse\eclipse-workspace\VoluntaryPullFromEternity\target\VoluntaryPullFromEternity-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) # VoluntaryPullFromEternity ---
[INFO] Installing C:\Users\Roboverse\eclipse-workspace\VoluntaryPullFromEternity\target\VoluntaryPullFromEternity-0.0.1-SNAPSHOT.jar to C:\Users\Roboverse\.m2\repository\TromboneSMP\VoluntaryPullFromEternity\0.0.1-SNAPSHOT\VoluntaryPullFromEternity-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\Roboverse\eclipse-workspace\VoluntaryPullFromEternity\pom.xml to C:\Users\Roboverse\.m2\repository\TromboneSMP\VoluntaryPullFromEternity\0.0.1-SNAPSHOT\VoluntaryPullFromEternity-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.045 s
[INFO] Finished at: 2021-03-01T18:46:45-05:00
[INFO] ------------------------------------------------------------------------
Here is 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TromboneSMP</groupId>
<artifactId>VoluntaryPullFromEternity</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.4-R0.1-SNAPSHOT</version><!--change this value depending on the version-->
<type>jar</type>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
The output says
Building jar: C:\Users\Roboverse\eclipse-workspace\VoluntaryPullFromEternity\target\VoluntaryPullFromEternity-0.0.1-SNAPSHOT.jar
So if you do not see a JAR in target, you probably need to refresh your Eclipse. Have a look in the Windows Explorer (or some Linux equivalent) if you can find the file.

How to export packages and classes to a test module in a Java app?

I want to build a multi-modules SpringBoot app with following modules :
App : the Parent module, Main class is here
Model : all Domain objects, no Main class, no tests
Tests : dedicated to whole app tests, will have Cucumber tests
My problem is :
I actually can run unit tests on Model's objects in Tests module, but doing a mvn clean install on App module fails because Tests module doesn't know anything about Model's objects.
In one hand, Spring makes the job, importing all needed classes
from Model module and running tests on them.
In the other hand, Maven cries with the following error :
package com.my-app.model.my-package does not exist.
OK, Spring and Maven are 2 different tools but what should I do in the App's POM to explicitely declare that all objects from the Model module should be exported to Tests module in order to be tested ?
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.humanresources.game</groupId>
<artifactId>hrg-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Application</name>
<description>Human Resources game engine</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<modules>
<module>../hrg-model</module>
<module>../hrg-tests</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<mainClass>HrgAppApplication</mainClass>
<goal>package</goal>
</configuration>
</plugin>
</plugins>
</build>
</project>
The Tests' module POM only declares a <dependency> block on Model Module.
Neither Tests module nor Model one have any <build> block in their respective POM.
As seen here, everything should only be managed by App's POM.
So my question is : what should I add in the App's POM <build> block to effectively export all Model's packages and classes to Tests module so that it remains possible to mvn clean install the whole project ?
Thanx by advance.
Edit : POM.xml for Tests and Model modules
Tests / POM
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.humanresources.game</groupId>
<artifactId>hrg-app</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../hrg-app/pom.xml</relativePath>
</parent>
<artifactId>hrg-tests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Tests</name>
<description>Tests project</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.humanresources.game</groupId>
<artifactId>hrg-model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Model / POM
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.humanresources.game</groupId>
<artifactId>hrg-app</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../hrg-app/pom.xml</relativePath>
</parent>
<artifactId>hrg-model</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Model</name>
<description>Tests project for Human Resources game</description>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
Console Output after mvn clean install (unit test succeeds though) :
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] Application [pom]
[INFO] Model [jar]
[INFO] Tests [jar]
[INFO]
[INFO] ------------------< com.humanresources.game:hrg-app >-------------------
[INFO] Building Application 1.0-SNAPSHOT [1/3]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) # hrg-app ---
[INFO]
[INFO] --- spring-boot-maven-plugin:2.4.3:repackage (repackage) # hrg-app ---
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) # hrg-app ---
[INFO] Installing C:\Users\Fred\IdeaProjects\HumanResources\hrg-app\pom.xml to C:\Users\Fred\.m2\repository\com\humanresources\game\hrg-app\1.0-SNAPSHOT\hrg-app-1.0-SNAPSHOT.pom
[INFO]
[INFO] -----------------< com.humanresources.game:hrg-model >------------------
[INFO] Building Model 0.0.1-SNAPSHOT [2/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) # hrg-model ---
[INFO] Deleting C:\Users\Fred\IdeaProjects\HumanResources\hrg-model\target
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) # hrg-model ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) # hrg-model ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 11 source files to C:\Users\Fred\IdeaProjects\HumanResources\hrg-model\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) # hrg-model ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory C:\Users\Fred\IdeaProjects\HumanResources\hrg-model\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) # hrg-model ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) # hrg-model ---
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) # hrg-model ---
[INFO] Building jar: C:\Users\Fred\IdeaProjects\HumanResources\hrg-model\target\hrg-model-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.4.3:repackage (repackage) # hrg-model ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) # hrg-model ---
[INFO] Installing C:\Users\Fred\IdeaProjects\HumanResources\hrg-model\target\hrg-model-0.0.1-SNAPSHOT.jar to C:\Users\Fred\.m2\repository\com\humanresources\game\hrg-model\0.0.1-SNAPSHOT\hrg-model-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\Fred\IdeaProjects\HumanResources\hrg-model\pom.xml to C:\Users\Fred\.m2\repository\com\humanresources\game\hrg-model\0.0.1-SNAPSHOT\hrg-model-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] -----------------< com.humanresources.game:hrg-tests >------------------
[INFO] Building Tests 0.0.1-SNAPSHOT [3/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) # hrg-tests ---
[INFO] Deleting C:\Users\Fred\IdeaProjects\HumanResources\hrg-tests\target
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) # hrg-tests ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) # hrg-tests ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\Fred\IdeaProjects\HumanResources\hrg-tests\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) # hrg-tests ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory C:\Users\Fred\IdeaProjects\HumanResources\hrg-tests\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) # hrg-tests ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\Fred\IdeaProjects\HumanResources\hrg-tests\target\test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/Fred/IdeaProjects/HumanResources/hrg-tests/src/test/java/com/humanresources/game/hrgtests/HrgTestsApplicationTests.java:[3,42] package com.humanresources.game.model.card does not exist
[ERROR] /C:/Users/Fred/IdeaProjects/HumanResources/hrg-tests/src/test/java/com/humanresources/game/hrgtests/HrgTestsApplicationTests.java:[4,47] package com.humanresources.game.model.territory does not exist
[ERROR] /C:/Users/Fred/IdeaProjects/HumanResources/hrg-tests/src/test/java/com/humanresources/game/hrgtests/HrgTestsApplicationTests.java:[5,47] package com.humanresources.game.model.territory does not exist
[INFO] 3 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Application 1.0-SNAPSHOT ........................... SUCCESS [ 1.533 s]
[INFO] Model 0.0.1-SNAPSHOT ............................... SUCCESS [ 5.104 s]
[INFO] Tests 0.0.1-SNAPSHOT ............................... FAILURE [ 3.715 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.771 s
[INFO] Finished at: 2021-02-21T18:33:11+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile (default-testCompile) on project hrg-tests: Compilation failure: Compilation failure:
[ERROR] /C:/Users/Fred/IdeaProjects/HumanResources/hrg-tests/src/test/java/com/humanresources/game/hrgtests/HrgTestsApplicationTests.java:[3,42] package com.humanresources.game.model.card does not exist
[ERROR] /C:/Users/Fred/IdeaProjects/HumanResources/hrg-tests/src/test/java/com/humanresources/game/hrgtests/HrgTestsApplicationTests.java:[4,47] package com.humanresources.game.model.territory does not exist
[ERROR] /C:/Users/Fred/IdeaProjects/HumanResources/hrg-tests/src/test/java/com/humanresources/game/hrgtests/HrgTestsApplicationTests.java:[5,47] package com.humanresources.game.model.territory does not exist
See a screen capture after mvn clean install :
Problem Diagnostics
The output for the Maven execution contains the following lines for the hrg-model:
[INFO] --- spring-boot-maven-plugin:2.4.3:repackage (repackage) # hrg-model ---
[INFO] Replacing main artifact with repackaged archive
The JAR artifact of hrg-model is not a usual library after that, but an executable JAR packaged as described in the Spring Boot documentation
That executable JAR is not suitable for consumption by a Java compiler - or compiler for any other JVM-based language for that matter.
Solution
Only the Maven module which represents the final executable artifact of the project should include the invocation of the spring-boot-maven-plugin.
Please note, that your project has no such module at the moment.
I would suggest:
adding the new module with hrg-app as a parent and hrg-model as a dependency;
moving the spring-boot-maven-plugin invocation from the hrg-app module to the new module.
You may check the official guide from Spring Boot - Creating a Multi Module Project - for a step-by-step guide and explanation.
Try adding this plugin in your APPs pom.xml under plugins section of the XML's build element.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version><--your maven version--></version>
</plugin>

How to pack properties file in jar using maven

I have been trying to pack the properties file inside my jar using Maven but no luck so far.
Project Structure:
src/main/java
All java source files
src/test/java
All test java source files
src/main/resource
All properties files. For example, application.properties
...
pom.xml
POM.XML File Contents:
<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>Database</groupId>
<artifactId>Executor</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>DatabaseExecutor</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.framework.version>5.2.0.RELEASE</spring.framework.version>
<spring.framework.group.id>org.springframework</spring.framework.group.id>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<fully.qualified.main.class>com.applications.App</fully.qualified.main.class>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${spring.framework.group.id}</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>${spring.framework.group.id}</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>${spring.framework.group.id}</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>${spring.framework.group.id}</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>${spring.framework.group.id}</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>12.2.0.1</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/libs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>3.2.0</version>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>${fully.qualified.main.class}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
I have created a build.bat file to build the code having the below content.
#ECHO OFF
call mvn -e pre-clean
call mvn -e clean
call mvn -e compile
call mvn -e package -DskipTests
Command Prompt Logs:
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< Database:Executor >--------------------------
[INFO] Building DatabaseExecutor 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.146 s
[INFO] Finished at: 2021-02-02T19:10:26+05:30
[INFO] ------------------------------------------------------------------------
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< Database:Executor >--------------------------
[INFO] Building DatabaseExecutor 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) # Executor ---
[INFO] Deleting C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.465 s
[INFO] Finished at: 2021-02-02T19:10:28+05:30
[INFO] ------------------------------------------------------------------------
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< Database:Executor >--------------------------
[INFO] Building DatabaseExecutor 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # Executor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # Executor ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.165 s
[INFO] Finished at: 2021-02-02T19:10:33+05:30
[INFO] ------------------------------------------------------------------------
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< Database:Executor >--------------------------
[INFO] Building DatabaseExecutor 0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # Executor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # Executor ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # Executor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) # Executor ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) # Executor ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-dependency-plugin:3.1.2:copy-dependencies (copy-dependencies) # Executor ---
[INFO] Copying log4j-api-2.14.0.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\log4j-api-2.14.0.jar
[INFO] Copying log4j-core-2.14.0.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\log4j-core-2.14.0.jar
[INFO] Copying junit-3.8.1.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\junit-3.8.1.jar
[INFO] Copying spring-core-5.2.0.RELEASE.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\spring-core-5.2.0.RELEASE.jar
[INFO] Copying spring-jcl-5.2.0.RELEASE.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\spring-jcl-5.2.0.RELEASE.jar
[INFO] Copying spring-context-5.2.0.RELEASE.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\spring-context-5.2.0.RELEASE.jar
[INFO] Copying spring-aop-5.2.0.RELEASE.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\spring-aop-5.2.0.RELEASE.jar
[INFO] Copying spring-beans-5.2.0.RELEASE.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\spring-beans-5.2.0.RELEASE.jar
[INFO] Copying spring-expression-5.2.0.RELEASE.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\spring-expression-5.2.0.RELEASE.jar
[INFO] Copying ojdbc14-12.2.0.1.jar to C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\libs\ojdbc14-12.2.0.1.jar
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) # Executor ---
[INFO] Building jar: C:\Users\VarunJain\Documents\Personal\DevEnv\_Setups\PracticeWorkspace\DatabaseExecutor\target\Executor-0.1.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.292 s
[INFO] Finished at: 2021-02-02T19:10:39+05:30
[INFO] ------------------------------------------------------------------------
Though the executable jar file is getting created successfully but the properties file is missing in the jar file. I'm not sure what is the mistake here. Can somebody please point out the mistake or tell me if I missed something.
Thanks In Advance,
Varun Jain
I resolved it myself by creating the new project and copying the contents from this project to the newly created project.

Maven generating actual jar as .jar.original instead of .jar file

Basically I have a Maven Project and I am trying to generate a jar of my project using mvn clean compile package through spring tool suite using below pom.xml.
After the command is executed, I get 2 jar files generated in my target folder - {project-name}.jar & {project-name}.jar.original
The .jar file contains
Spring boot framework class files having this contents while the .jar.original file contains actual class files of my application.
Snapshot of target folder
I also tried using mvn clean install but no luck with it. Moreover, the only 1 file is installed which is useless for me, because it does not contain any class files of my application but contains spring framework classes.
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>com.commonporject</groupId>
<artifactId>commonporject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>commonporject</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc4</artifactId>
<version>10.1</version>
</dependency>
<!-- other dependencies -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerVersion>1.8</compilerVersion>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Console:
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.commonporject:commonporject >----------------------
[INFO] Building commonporject 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) # commonporject ---
[INFO] Deleting C:\Users\Aadil\Documents\workspace-spring-tool-suite-4-4.0.0.RELEASE\commonporject\target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) # commonporject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) # commonporject ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to C:\Users\Aadil\Documents\workspace-spring-tool-suite-4-4.0.0.RELEASE\commonporject\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) # commonporject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) # commonporject ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) # commonporject ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) # commonporject ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) # commonporject ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.1.0:jar (default-jar) # commonporject ---
[INFO] Building jar: C:\Users\Aadil\Documents\workspace-spring-tool-suite-4-4.0.0.RELEASE\commonporject\target\commonporject-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.0.RELEASE:repackage (repackage) # commonporject ---
[INFO] Replacing main artifact C:\Users\Aadil\Documents\workspace-spring-tool-suite-4-4.0.0.RELEASE\commonporject\target\commonporject-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.706 s
[INFO] Finished at: 2018-11-04T12:28:37+05:30
[INFO] ------------------------------------------------------------------------
spring-boot-maven plugin by default uses the repackage goal which packages all dependencies mentioned in the pom file. When this happens, your project's classes and resources jar is renamed to "original".
Please refer the below documentation for more details about this plugin.
https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html

Maven will not find junit tests to execute, says it ran 0 tests and the build succeeded

Firstly, why does maven come with a test goal, but also I see people running mvn surefire:test as if it does something else. What is the difference between the two?
Secondly, I cannot seem to get maven to run my unit tests. No matter what I try, I always get Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 . My myproject/target/generated-tests-sources/test-annotations directory is empty, but my myproject/target/test-classes directory contains a .class file for each of my test classes.
Project Structure:
myproject/src/main/java/MyClass.java
myproject/src/test/java/MyClassTest.java
myproject/pom.xml
Sample MyclassTest.java Content:
import org.junit.jupiter.api.Test;
#TestInstance(TestInstance.Lifecycle.PER_CLASS)
class MyClassTest {
#Test
void testFunction() {
assertEquals(0, 0);
}
}
Contents of 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>co.blocanse.pa.myproject</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Output of mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) # myproject ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 7 source files to D:\projects\myproject\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.141 s
[INFO] Finished at: 2018-01-01T23:32:27-06:00
[INFO] Final Memory: 14M/213M
[INFO] ------------------------------------------------------------------------
Output of mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) # myproject ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) # myproject ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to D:\projects\myproject\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) # myproject ---
[INFO] Surefire report directory: D:\projects\myproject\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.362 s
[INFO] Finished at: 2018-01-01T23:38:07-06:00
[INFO] Final Memory: 15M/213M
[INFO] ------------------------------------------------------------------------
Here's where the issue lies. I would expect the test results to say that it ran 1 test, but it claims it went through and ran none of them, while still succeeding the build. mvn surefire:test yields the same outcome. My apologies for the large amount of copy-pasted content, but I want to provide as much information as possible because I'm not sure what to try next. If it helps, I'm on Windows and using IntelliJ IDEA.
org.junit.jupiter.api.Test is junit 5 (which may not be supported fully yet), and not junit 4. Remove the jupiter dependency and use junit 4 for now. A sample jUnit 4 test from https://github.com/junit-team/junit4/wiki/getting-started:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculatorTest {
#Test
public void evaluatesExpression() {
Calculator calculator = new Calculator();
int sum = calculator.evaluate("1+2+3");
assertEquals(6, sum);
}
}

Categories