Maven system scope for testing purposes - java

I wrote a little framework for testing microservices. Now I packed my contracts into a jar which will be used to test my framework, but i don't want to deploy them to our nexus. so my pom contains
<dependency>
<groupId>mycompany.testframework</groupId>
<artifactId>test.dummy-contract</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/test/resources/contract-dummy.jar</systemPath>
</dependency>
now if i want to use the framework in another project (using gradle) I get this error message:
Processing of C:\Users\dam\.gradle\caches\modules-2\files-2.1\mycompany.testframework\TestFramework\0.0.1-SNAPSHOT\5859a002118d47b4237425d25dfc79ea1d7eb829\TestFramework-0.0.1-SNAPSHOT.pom failed:
'dependencies.dependency.systemPath' for mycompany:test.dummy-contract:jar must specify an absolute path but is ${project.basedir}/src/test/resources/contract-dummy.jar in mycompany.testframework:TestFramework:0.0.1-SNAPSHOT
I need the dependency for my jenkins test and build but not for "delivery". any suggestions?

I resolved it by adding the dependency to a profile wich only will be used for the test-framework and on the jenkins
<profiles>
<profile>
<id>test-dummy</id>
<activation>
<file>
<exists>${project.basedir}/src/test/resources/contract-dummy.jar</exists>
</file>
</activation>
<dependencies>
<dependency>
<groupId>de.company.testframework</groupId>
<artifactId>test.dummy-contract</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/test/resources/contract-dummy.jar</systemPath>
<optional>true</optional>
</dependency>
</dependencies>
</profile>
</profiles>

Related

Is it possible to turn off (prevent activation) of a maven profile with gradle? - (unwanted maven profile active)

I have an issue with the barcode library zxing.
compile('com.google.zxing:core:3.4.1') compile('com.google.zxing:javase:3.4.1')
The com.google.zxing core-3.4.1.pom is refering to a parent:
<parent>
<groupId>com.google.zxing</groupId>
<artifactId>zxing-parent</artifactId>
<version>3.4.1</version>
</parent>
..and the zxing-parent-3.4.1.pom contains this snippet
<profiles>
<profile>
<id>build-android</id>
<activation>
<property>
<name>env.ANDROID_HOME</name>
</property>
<jdk>[,9)</jdk> <!-- Android won't necessarily work with JDK 9 -->
</activation>
<modules>
<module>android-core</module>
<module>android-integration</module>
<module>android</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>${android.platform}</version>
<scope>system</scope>
<!-- ANDROID_HOME must be absolute, but redundant leading / may help Gradle Spring Boot plugin -->
<systemPath>/${android.home}/platforms/android-${android.platform}/android.jar</systemPath>
</dependency>
</dependencies>
</dependencyManagement>
</profile>
<profile>
Now the missing systemPath leads to this error:
Task :compileJava
Errors occurred while build effective model from C:\Users\eXtensia.gradle\caches\modules-2\files-2.1\com.google.zxing\core\3.4.1\d1e7c4667f9dcdda30d09f3df7d1b44f65c44336\core-3.4.1.pom:
'dependencyManagement.dependencies.dependency.systemPath' for com.google.android:android:jar must specify an absolute path but is ${env.ANDROID_HOME}/platforms/android-22/android.jar in com.google.zxing:core:3.4.1
Errors occurred while build effective model from C:\Users\eXtensia.gradle\caches\modules-2\files-2.1\com.google.zxing\javase\3.4.1\357121cdf3f89f63ddfaad18170612dcad0db947\javase-3.4.1.pom:
'dependencyManagement.dependencies.dependency.systemPath' for com.google.android:android:jar must specify an absolute path but is ${env.ANDROID_HOME}/platforms/android-22/android.jar in com.google.zxing:javase:3.4.1
Question
Gradle: is it possible to turn off this profile with gradle as android is not needed?
Gradle: Other ways to manipulate the downloaded .pom automatically via gradle to remove this profile?
Maven: Suggestion how to change the zxing-parent-3.4.1.pom that it will continue to run for android users, non android users and maven/gradle users? (there is already an issue and there are chances to get it changed.)

Multi module project to deliver two different wars

I am working on a multi module Spring MVC project in which one module has to deliver a war with all dependencies and other has to deliver a war with few dependencies excluded. Is this possible? If yes how can I achieve this? The project details are as below:
The structure is:
Parent pom:
<modules>
<web-war-with-all-dependencies>
<web-ear-without-dependencies> --> Only to pack the war into an ear.
</modules>
A shared library is created in Websphere and all dependencies are added there. So, < web-ear-without-dependencies > will be deployed there.
< web-war-with-all-dependencies > will be used to build a docker tomcat image and hosted in a diff environment.
My project has to support both environments. Hence the weird requirement.
Using Maven profiles and by adding some dependencies only in some profile or setting them with scope: provided:
https://www.baeldung.com/maven-profiles
Example:
<profiles>
<profile>
<id>dev</id>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${testcontainers.version}</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>prod</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<!-- my own library in my private repository which acts like a mock of testcontainers project. Don't ask :-) -->
<dependency>
<groupId>cz.jiripinkas</groupId>
<artifactId>fake-testcontainers</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</profile>
</profiles>
And then you build your project using:
mvn clean install -P dev
This will put inside WAR testcontainers libraries
or:
mvn clean install -P prod
This will put inside WAR fake-testcontainer library

How to run app as jar in IDE, but package with maven as war?

I have an application with a normal main method, running as a normal jar within my IDE. It's a spring-boot application that runs on an embedded tomcat by default.
To package it as war file with maven is as simple as:
<packaging>war</packaging>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Problem: I'd still would like to run the app locally in my IDE embedded. Thus exclude the dependency above, and leave packaging to >jar<.
Question: is it possible to eg define profiles in maven, so that by default the packaging is =jar, but when running a specific profile it is exchanged with war and the tomcat server dependency is included?
Ok, it's as simple as:
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<packaging.type>jar</packaging.type>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<packaging.type>war</packaging.type>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
</profiles>
+for IntelliJ IDEA: running the appropriate maven command is as simple as: checking the desired profile an click on 'package'.

Java - Adding jar dependency in pom.xml

I have never built my java applications by maven. But when i am trying to do that it's giving me error.
I have created JAR file from other java application just by exporting as JAR from that application.
Now i want to add this JAR in my maven application. I don't really how to do that.
this is how i have added in pom.xml. But i don't really know what should be it's artifact id.
Seriously what is artifact id?
<dependency>
<groupId>ProjectZen</groupId>
<artifactId>community</artifactId>
<scope>system</scope>
<version>1</version>
<systemPath>${basedir}\libs\ProjectZen.jar</systemPath>
</dependency>
I am getting below error
Missing artifact ProjectZen:community:jar:1
Thanks
Fahad Mullaji
If it is custom jar you need to do following things
Open cmd and type following command
mvn install:install-file -Dfile=path-to-your-artifact-jar \
-DgroupId=ProjectZen
-DartifactId=community
-Dversion=1
-Dpackaging=jar
-DgeneratePom=true
Now, the “ProjectZen” jar is copied to your Maven local repository.
In pom.xml
<dependency>
<groupId>ProjectZen</groupId>
<artifactId>community</artifactId>
<scope>system</scope>
<version>1</version>
<systemPath>${basedir}\libs\ProjectZen.jar</systemPath>
</dependency>
now the “ProjectZen” jar is able to retrieve from your Maven local repository.
change
<systemPath>${basedir}\libs\ProjectZen.jar</systemPath>
to
<systemPath>${basedir}/libs/ProjectZen.jar</systemPath>
or install it in local maven cache
you should give the format as below. and the slashes used in are incorrect I suppose. Check the dependency in this format.
...
<profiles>
<profile>
<id>default-tools.jar</id>
<activation>
<property>
<name>java.vendor</name>
<value>Sun Microsystems Inc.</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>
Reference
...

What is wrong with my Maven Config?

I want to checkout sonar, so I added the following snippet to my pom.xml the dependency part was taken from http://maven.apache.org/general.html#tools-jar-dependency
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.jdbc.url>jdbc:derby://localhost:1527/sonar;create=true</sonar.jdbc.url>
<sonar.jdbc.driverClassName>org.apache.derby.jdbc.ClientDriver
</sonar.jdbc.driverClassName>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar</sonar.jdbc.password>
<sonar.host.url>http://localhost:8080/sonar</sonar.host.url>
</properties>
<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>
Unfortunatly the error persists
Embedded error: Missing:
----------
1) com.sun:tools:jar:1.4.2
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=com.sun -DartifactId=tools -Dversion=1.4.2 -Dpackaging=jar -Dfile=/path/to/file
I also followed the suggestion to add the missing jar manually to the repository, which had no effect.
mvn install:install-file -DgroupId=com.sun -DartifactId=tools -Dversion=1.4.2 -Dpackaging=jar -Dfile=$JAVA_HOME/lib/tools.jar
What am I doing wrong?
EDIT:
I verified that tools.jar has been added to my local repository. In debug mode maven shows the error:
1 required artifact is missing.
for artifact:
group:artifact:war:1.0.0-BUILD-SNAPSHOT
from the specified remote repositories:
central (http://repo1.maven.org/maven2)
Are you running this in eclipse? If the answer is yes, this is an annoying and very misunderstood problem. Take a look at my answer here
You may not be pointing eclipse to the right jre/jdk when you're starting up (this is something you didn't necessarily configure rather was Windows)
A problem I had once was different location of tools.jar under Mac OS. Here's the profiles section to solve the problem:
<profiles>
<profile>
<id>java-home-parent-lib-tools-jar</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${java.home}/../lib/tools.jar</exists>
</file>
</activation>
<dependencies>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
<profile>
<id>java-home-parent-classes-classes-jar</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${java.home}/../Classes/classes.jar</exists>
</file>
</activation>
<dependencies>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../Classes/classes.jar</systemPath>
</dependency>
</dependencies>
</profile>
</profiles>
However I am not sure this is something you're facing.
If your JAVA_HOME points to your jdk (e.g./usr/lib/jvm/jdk1.6.0_33), your tools.jar configuration (${java.home}/../lib/tools.jar) indicates that there should be a tools jar with the following path: /usr/lib/jvm/lib/tools.jar.
If you change the tools jar path to ${java.home}/lib/tools.jar and verify that in your JAVA_HOME/lib there is the tools.jar file, it should work.
There you can find the related jira.

Categories

Resources