including test-jar in the runtime scope - maven

i got a war-project and in the test-jar of it, we have aside of the jUnit testcases also the mocks to the neighbor systems (for instance, the roles and users management system).
and we have a maven profile called mocking that adds the test-jar dependency to the war-project, at runtime, so that the mocks are available for the developer, but do not end up by error in production.
<profile>
<id>mocking</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>runtime</scope>
</dependency>
...
not very clean, i know, but we did not want to have just another artifact for only a hand full of mock clases, and it worked so far well with Maven 3.3.9.
now we need a feature of Maven 3.5.0, so i updated to the latest Maven 3.6.2 and get following error:
The project com.my-project:web:0.0.1-SNAPSHOT has 1 error:
'dependencies.dependency.[com.my-project:web:0.0.1-SNAPSHOT]' for com.my-project:web:0.0.1-SNAPSHOT is referencing itself.
which is kind of borderline case.
imho, and since the dependency is of scope runtime, it should be allowed.
is this a bug?
can anyone figure out a better way to achieve this?
many thanks
Michael
UPDATE 20191125:
Without full pom files or at least an example which looks very like your original projects it's hard to guess....
Here a small pom-file to reproduce the problem:
<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>my.project</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</name>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
with that file, and nothing else, mvn compile works fine with v3.3.9 but breaks with v3.6.2

If you use the normal setup for creating a test-jar this implies the test-jar is created as supplemental artifact to your usual artifacts which needed to be distinguished from each other which has to be achieved by using the <classifier>tests</classifier>.

Related

IDEA import of Maven GitHub projects not working/running -> "Cannot resolve symbol"

#### UPDATE v2 ####
Ok I found out the issue, it was in fact an IDEA bug. More precisely, it's the git extension, gitflowincrementalbuilder, which from 3.8+ breaks Idea. Changing version to 3.7 solves it, for now.
https://github.com/vackosar/gitflow-incremental-builder/issues/91
Intellij/git, please fix it
------ Old Update v1 -----
I just tried running the project with Eclipse... works perfectly without any issues whatsoever, at first try... So it's kind of a Intellij-IDEA bug/problem (...)
I am trying to run some examples from Github, the spring spring-boot ones from baeldung.com; more specifically this one (no one works in idea): https://github.com/eugenp/tutorials/tree/master/spring-mvc-simple-2
While it works using Maven commands, "mvn clean install"
and then "mvn spring-boot:run", wont work in Idea (it does clean and install ok, but no run). Project is imported using "New"->"Project from Existing Sources" (check images below for settings).
I think there is some problem with the pom imported configuration, especially since there is a multi module structure (parent tag); cannot even resolve #SpringBootApplication.
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-mvc-simple-2</artifactId>
<packaging>war</packaging>
<name>spring-mvc-simple-2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
<finalName>spring-mvc-simple2</finalName>
</build>
I have been trying to add a SpringBoot configuration manually using IDEA gui but it doesnt recognize the application class (?). What partially works though is replacing the parent pom with:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Tests still wont work, but I dont think u're supposed to change/edit the pom file manually to make things works...
I have been trying alot already: invalidate cache, maven reimport & Generate sources and update folders, using mvn first, checking all jdk configuration... nothing works.
You didn't build the parent module so IntelliJ does not find this in your local Maven repository.
You should run mvn install in the project:
https://github.com/eugenp/tutorials/blob/master/parent-boot-2/pom.xml
But also exchanging the parent helps like you described yourself.
To make the tests run you have to add the test dependency from the parent:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
</dependency>

Compiling classes with dependent jar files in maven

I want to compile some java files which have multiple dependent jars and make a jar file. I have kept all dependent jars under src/main/lib. after running mvn clean install, i get compilation failure of the classes. Dependent jars are not being copied to class path it seems. Anyone can tell whats going wrong her.
<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>test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<repositories>
<repository>
<id>external-jars</id>
<name>external-jars</name>
<url>file://${project.basedir}\src\main\lib\</url>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>jars</groupId>
<artifactId>jars</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib/*</systemPath>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>finaljar</finalName>
<sourceDirectory></sourceDirectory>
</build>
</project>
You construction
<dependency>
<groupId>jars</groupId>
<artifactId>jars</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib/*</systemPath>
</dependency>
does not work. In Maven, each artifact is addressed through groupId, artifactId and version. You cannot add a directory, but only separate jars.
While it is possible to add jars through system paths, it is much better to use a repository. Look at the possibilities in
How to add local jar files to a Maven project?

Maven project with prebuilt jar

Currently I am not using Maven in any way whatsoever, but since I am writing a library I would like other developers to be able to use it as a Maven dependency. It seems like the easiest way to do this would be to have a Maven project which just contains the jar.
However, all the examples I've seen of pom.xml have build logic in them and I was wondering how I am supposed specify the prebuilt jar as the resulting artifact.
Thanks!
I do not know if I understand you correctly, so maybe I am commenting something totally different.
If you want to offer your project as Maven dependency for other projects, the project must be somewhere to be downloaded (Maven central repository, your company Nexus...) and your project must have something to identify it: groupId, artifactId and the version. You can specify these separately when installing the artifact, or you can provide a pom that contains this metadata, along with dependencies if there are any. Both approaches are explained here.
Then adding something like this in the project that wants your project as dependency, it should work.
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>1.0</version>
</dependency>
Still, since you control this project, you might want to consider converting your library to a Maven project to reap other benefits of Maven (in some IDEs like Eclipse there is an option that convert a regular Java Project in a Maven project), and in the generated pom.xml is the info which is mention above.
Here an example of a basic pom.xml to be a jar.
<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.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Extracted from here --> http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Trouble converting EAR (and its WARs) to Maven

Today I thought it was a good idea to convert my projects into Maven projects.
I have an EAR that contains 4 WARs and 3 EJB-modules. I followed IBM's tutorial about migrating an EAR project. I ended up converting all my wars/ejb-jars one by one, and finally the EAR. I also added the war to the EAR as dependencies in the EAR's pom.xml. But when I launch the mvn ear:ear command it throws me a warning like this one :
[WARNING] The POM for server-admin-connector:server-admin-connector:war:0.0.1-SNAPSHOT is missing, no dependency information available
Then, the build fails because of this error :
[ERROR] Failed to execute goal on project server-ear: Could not resolve dependencies for project server-ear:server-ear:ear:0.0.1-SNAPSHOT: The following artifacts could not be resolved: server-admin-connector:server-admin-connector:war:0.0.1-SNAPSHOT
I think IBM's article is not complete, but I couldn't find any other source about migrating a whole EAR project to Maven. Can anyone help me on this? How can I get rid of this error?
EDIT : I don't have a particular project structure in my workspace. Here are my projects, however :
/server-project/
*server-ear (EAR)
*server-admin-connector (WAR)
*server-adminClient (JAR - stores the shared POJOs and interfaces for admin)
*server-business-layer (EJB)
*server-business-layerClient (JAR - stores the shared POJOs and interfaces for the business connector)
*server-business-connector (WAR)
I started converting the non-EAR projects, with their required dependencies. Then I used the maven ear module to add them all in the ear's POM.xml. Eclipse didn't show me any errors when doing this, so I assumed the build part was OK.
Here's the EAR's 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>server-ear</groupId>
<artifactId>server-ear</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>ear</packaging>
<name>server-ear</name>
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<configuration>
<earSourceDirectory>EarContent</earSourceDirectory>
<generateApplicationXml>true</generateApplicationXml>
<applicationXml>${project.build.directory}/application.xml</applicationXml>
<skinnyWars>true</skinnyWars>
<version>7</version>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<modules>
<ejbModule>
<groupId>server-business-layer</groupId>
<artifactId>server-business-layer</artifactId>
</ejbModule>
<webModule>
<groupId>server-admin-connector</groupId>
<artifactId>server-admin-connector</artifactId>
<contextRoot>/admin</contextRoot>
</webModule>
<webModule>
<groupId>server-intern-rest-connector</groupId>
<artifactId>server-intern-rest-connector</artifactId>
<contextRoot>/api</contextRoot>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>server-admin-connector</groupId>
<artifactId>server-admin-connector</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>server-business-layer</groupId>
<artifactId>server-business-layer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>ejb</type>
</dependency>
<dependency>
<groupId>server-intern-rest-connector</groupId>
<artifactId>server-intern-rest-connector</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>server-business-layerClient</groupId>
<artifactId>server-business-layerClient</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>server-adminClient</groupId>
<artifactId>server-adminClient</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
I found my problem. I wasn't installing every war/jar one by one. Don't forget to run the "mvn install" command on all WARs/JARs before trying to generate the EAR!
I don't know if I mark this as answer, or if i simply delete the question...

Different result by different profile activate method?

I have been using maven for about two years, but I don't think I understand profiles in maven exactly, especially when I encounter the following problem.
I have a maven project with three modules, secweb-parent, secweb-service and secweb-web, secweb-sevice depends on spring-webmvc, and secweb-web depends on secweb-service.
The problem is :
1) When I use 'mvn clean install -Dinclude', it works well, and spring-mvc.jar will be found in secweb-web.war
2) When I use 'mvn clean install -Pinclude-jar', it doesn't work, and spring-mvc.jar can't be found in secweb-web.war
Anybody know why ? And is there something I must pay attention to when I use profiles ?
(I know I can define scope of dependency, this project here just to demo different result of different profile activate method)
pom.xml for secweb-parent
<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>
<properties>
<project.version>1.0.0</project.version>
</properties>
<groupId>com.mediatek.dt</groupId>
<artifactId>secweb-parent</artifactId>
<version>${project.version}</version>
<packaging>pom</packaging>
<modules>
<module>../secweb-web</module>
<module>../secweb-service</module>
</modules>
</project>
pom.xml for secweb-service
<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>
<parent>
<groupId>com.mediatek.dt</groupId>
<artifactId>secweb-parent</artifactId>
<version>${project.version}</version>
<relativePath>../secweb-parent</relativePath>
</parent>
<artifactId>secweb-service</artifactId>
<profiles>
<profile>
<id>include-jar</id>
<activation>
<property>
<name>include</name>
</property>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
pom.xml for secweb-web
<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>
<parent>
<groupId>com.mediatek.dt</groupId>
<artifactId>secweb-parent</artifactId>
<version>${project.version}</version>
<relativePath>../secweb-parent</relativePath>
</parent>
<artifactId>secweb-web</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.mediatek.dt</groupId>
<artifactId>secweb-service</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
You mat check the active profile by using the Maven Help Plugin as the following example
change directory to the parent
e.g. cd /my/project/secweb-parent
Then execute the following command twice for comparing
mvn help:help:active-profiles
mvn help:help:active-profiles -Pinclude-jar
Anyhow the significant point is the version, you have defined as 1.0.0. I would prefer to use the 1.0.0-SNAPSHOT instead.
You may see further information about the SNAPSHOT, here.
Why would you use this? SNAPSHOT versions are used for projects under active development. If your project depends on a software component that is under active development, you can depend on a SNAPSHOT release, and Maven will periodically attempt to download the latest snapshot from a repository when you run a build. Similarly, if the next release of your system is going to have a version "1.4", your project would have a version "1.4-SNAPSHOT" until it was formally released.
As a default setting, Maven will not check for SNAPSHOT releases on remote repositories. To depend on SNAPSHOT releases, users must explicitly enable the ability to download snapshots using a repository or pluginRepository element in the POM.
There is also a useful question and answer about the snapshot at our stackoverflow, here as well.
I hope this may help.

Resources