Java - Adding jar dependency in pom.xml - java

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
...

Related

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

Dynamically add a new maven dependency?

I need to add a new maven dependency to my maven project when project is built by the continuous integration server, but the dependency should not be there when developers are building the project locally.
Is there a way to dynamically add the dependency through a maven plugin so that continuous integration plan can run a maven command and add the dependency by itself?
Using profiles is the best way for this kind of case
Here is the example to customize the dependencies inclusion
<profiles>
<profile>
<id>profile-dev</id>
<dependencies>
<dependency>
depednency A
</dependency>
</dependencies>
</profile>
<profile>
<id>profile-prod</id>
<dependencies>
<dependency>
dependency B
</dependency>
</dependencies>
</profile>
<profiles>
To run the build at dev box mvn install -P profile-dev
To run the build at production mvn install -P profile-prod

missing tools.jar error on Update Maven Project

When i try to update maven project I get the error that maven cant find tools.jar in JRE locations shown below:
However I have installed and specefied JDK in Windows->Preferences->Java->Installed JRE.
Is there any other place the JDK should be specified in order to resolved this tools.jar issues.
Note: tools.jar is located in lib of JDK_HOME directory and i also specified the jar as mentioned in this answer but it did not resolve issue.
Go to pom.xml and then:
<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>
</profiles>

Maven system scope for testing purposes

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>

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