Maven: Including external JAR in POM - maven

I know questions like this have been asked and answered before, and have taken the time to read those threads, but somehow they won't help me. I have locally added own Java code to the DSpace software, and my code depends on another library, so I'll have to include this library (JAR) into the <dependencies> section of the POM, right? This is what my entry looks like:
<dependency>
<groupId>de.mannheim.ids</groupId>
<artifactId>pid</artifactId>
<version>0.0.1-SNAPSHOT</version>
<systemPath>/absolute/path/to/jar/pid-client-0.0.1-SNAPSHOT.jar</systemPath>
<scope>system</scope>
</dependency>
This does not help but results in a Compile Error - the relevant classes cannot be found. I have also tried mvn install:install-file -Dfile..., which tells me everything is fine with the jar (BUILD SUCCESS), but the subsequent mvn package fails with the usual error.
What could I be doing wrong?

You should be able to use the artifact as usual after using mvn install:install.
If that command returned Build successful you should be able to use this in your pom:
<dependency>
<groupId>de.mannheim.ids</groupId>
<artifactId>pid</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Maven will find it like it would find any jar in your local repository.

Related

selenium-central-framework in pom.xml

I am not able to find any hint about the issue that I am facing and hence posting my question here. Please apologize if it is something silly.
I have some working experience in selenium webdriver. However in my new project, I was asked to use an existing Selenium framework.
In the pom.xml, I am seeing the below dependency (I have edited xxx to avoid displaying the company name)
<dependencies>
<dependency>
<groupId>com.xxxqa.selenium</groupId>
<artifactId>selenium-central-framework</artifactId>
<version>2.0.18</version>
</dependency>
Question :
I couldn't understand from where that dependancy was taken from ?
Solution Tried :
I have tried searching in the maven repository still couldn't find
anything related to "selenium-central-framework"
I have checked the dependencies of selenium RC, but they seems to be different from the one that is present above.
I have researched whether any local jars were used and were linked
as dependancy. However I understand from maven repository, local
repositories will be linked with the tag "systemPath"
<dependency>
<groupId>ldapjdk</groupId>
<artifactId>ldapjdk</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${basedir}\src\lib\ldapjdk.jar</systemPath>
</dependency>
</dependencies>
Note that the depedency used in my project doesn't have the tag "systemPath".
Is there anyway the external repositories could be added without mentioning systemPath ??
Please help, what could be that dependency ? How to find more information about it. Thanks a ton for your help.
I suggest you one of these approaches:
Jar file
If you have access to the jar file called : selenium-central-framework-2.0.18.jar, open a command line pointing to the folder of this jar and execute:
mvn install:install-file \
-Dfile=selenium-central-framework-2.0.18.jar \
-DgroupId=com.xxxqa.selenium \
-DartifactId=selenium-central-framework \
-Dversion=2.0.18 -Dpackaging=jar
Source code file
If you have the source code of : selenium-central-framework-2.0.18.jar, open a command line pointing to the folder of this source code and execute:
mvn clean install
After one of these approaches, selenium-central-framework-2.0.18.jar will be available in you local maven repository ($HOME/.m2) and your other java maven projects will be ready to use it as dependency:
<dependency>
<groupId>com.xxxqa.selenium</groupId>
<artifactId>selenium-central-framework</artifactId>
<version>2.0.18</version>
</dependency>
This is a workaround if you don't have a server to host an artifact repository management server like :
https://www.sonatype.com/nexus-repository-sonatype
https://www.jfrog.com/confluence/display/RTF/Maven+Repository
Explanation
Almost all free, public, safe, useful and cool java libraries are hosted in https://mvnrepository.com . So any person in the world can use it in their maven project with this piece in their pom.xml:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
But in some cases, publish your source code to https://mvnrepository.com is not an option:
Oracle Driver. This driver (.jar) can only be downloaded from the official Oracle page. Private source code like IBM, Microsfot,etc
Source code of your company that should not be public.
Some super cool library that is only in github but not in the maven central repository.
So in this cases, the best and scalable solution is host and configure some artifact repository management server like :
https://www.sonatype.com/nexus-repository-sonatype
https://www.jfrog.com/confluence/display/RTF/Maven+Repository
This platforms are an emulation of https://mvnrepository.com . So with a minimal configuration:
You can host your special or private jars
Your maven projects could use this jars with the standrad xml dependency in pom.xml.
If you don't have a server to implement one of these platforms, the previous approaches could help you and get the same results

how to add my one project jar in another maven project in netbeans

I have one project jar oauth.
I want to add it in another maven project . I tried to change pom.xml file but no effect. Can anyone please suggest me?
I tried to add following dependency in my pom.xml file:
<dependency>
<groupId>com.payupaisa.oauth</groupId>
<artifactId>auth</artifactId>
<version>1.0.0</version>
<type>jar</type>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapps/WEB-INF/lib/auth.jar</systemPath>
</dependency>
With the assumption that you have that auth.jar in your local repository (as it builds fine).
Why don't you give a try like this.
<dependency>
<groupId>com.payupaisa.oauth</groupId>
<artifactId>auth</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
Honestly speaking I don't prefer to give the jar location in my pom file and using scope as system, I leave this task to handle by Maven to resolve all the artifacts either by searching in local maven repository first(/.m2) or in MAVEN CENTRAL REPO if it is a 3rd party jar.

Use maven to "extend" existing jar file

My project contains a couple of class which have to be integrated into an final jar file.
So my current "workflow" looks like:
mvn compile && jar uf contribution.jar -C target/classes .
I guess this could be done with the maven-jar plugin or through maven-assemblies but can't figure out how.
Simple - add the element <packaging>jar</packaging> to your pom.xml and invoke the following:
mvn package
See Maven build lifecycle for more information. It really is a must-read if you're planning to use Maven for you project.
Edit
Thanks for the update, I know understand what you mean. There may be a better way to do this, but this is what I use:
Maven will automatically include this jar if you add it as a dependency in the usual way, i.e:
<dependencies>
...
<dependency>
<groupId>some.group</groupId>
<artifactId>contribution</artifactId>
<version>1.0.0</version>
</dependency>
...
</dependencies>
The question is how to ensure that maven can find the jar? If the jar were in the Maven Global repository it would find it automatically, so always check this first. If not, then you will have to create your own repository to store custom jars. This may not be the most clever way to do it, but it's how I do it. Note that this is not the same thing as the cache stored in the .m2 folder.
Let's say we want to use c:\maven\repo as our local. To do this, first ensure the folder exists then add the following lines to your pom.xml:
<repositories>
<repository>
<id>my-repo</id>
<url>file://c:\maven\repo</url>
</repository>
</repositories>
Next, you will need to manually add the contribution jar to this repo. To do this execute the mvn deploy:deploy-file command and give it the appropriate values. See here for more information.
Now when build/compile/package your original project, it should include contribution.jar in the packaging as it is a dependency.
Hope this helps :)

Skipping Maven Test Dependency

I am working on a project that use maven for building. What I am trying to do is to skip the test dependency. Basically running the maven build without the presence of artifact in my maven repository.
eg
<artifactId>example</artifactId>
<scope>test</scope>
This is from the pom file of my project and I have to maven build my project without having artifact example.
I have searched for solution such as use "-DskipTests=true" or "-Dmaven.test.skip=true". In my case they did skip the running of the tests but it still complains missing dependency file.
Does anyone know a way to run maven build without having to have test artifact in the maven repository?
Thanks.
See https://issues.apache.org/jira/browse/MNG-4192. I think the only way around it is to move the test-scoped dependency into a Maven profile.
Here is an example:
<profile>
<id>test-with-extra-dependency</id>
<dependencies>
<dependency>
<groupId>org.example.groupid</groupId>
<artifactId>artifact-id</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
You should now be able to run a build without tests:
mvn clean install -Dmaven.test.skip=true -DskipTests=true
After that you can run the tests with the profile enabled:
mvn test --activate-profiles test-with-extra-dependency
I wouldn't advise it, but this can be useful in case of a circular dependency between the current module and the test dependency, i.e. you can build the module, then build the test dependency using the build result, and finally test the module with the test dependency. If you find yourself wanting to do that, please try to restructure your project to eliminate the circular dependency (eg by moving the tests to a third module).

How to generate Javadoc for Maven Dependencies

I have a maven project with the following POM snippet:
<modelVersion>4.0.0</modelVersion>
<artifactId>Foo-Deploy</artifactId>
<name>Foo-Deploy</name>
<packaging>pom</packaging>
<description>foobar</description>
<dependencies>
<dependency>
<groupId>de.foo.bar</groupId>
<artifactId>some-api</artifactId>
<version>${project.version}</version>
<classifier>doc</classifier>
<type>zip</type>
</dependency>
</dependencies>
The idea is to have a dependency defined in which some sources are (this is created successfully before).
Now I want to run javadoc on exactly THIS dependency. When I call
mvn javadoc:jar -DincludeDependencySources=true -DdependencySourceIncludes=de.foo.bar:some-api:*:doc:zip
it fails with the message
Not executing Javadoc as the project
is not a Java classpath-capable
package
what is wrong ? and would it work anyhow ?
or how can I generate javadoc from a specific dependency (assuming this project has more dependencies) ?
Thanks
To generate javadoc for dependent sources, a sequence of steps needs to be done. These are outlined in this link.
Essentially you need to ensure that the source files of the dependency is generated/available and <includeDependencySources> parameter is enabled.

Resources