How Do I Point Maven To Java Source Code Dependencies? - java

I have an Apache Archiva Repo that I've set up to handle the binary dependencies for a Maven project that I'm working on; however, the project appears to also have some dependencies on local 3rd party source code (Unsure of Distribution License). Is there a way that I can put this source somewhere that Maven can grab it from when needing to do a build? Normally source would be stored in SCM, but that doesn't seem like the right solution for java source files that I'm only using to compile (only needed at compile time), but don't want in my main "/src" directory. Elaborations and best practices appreciated, I'm pretty new to Java Development and Maven.

Possible solutions:
Convince the 3rd party project to deploy the JAR with the class files to Maven Central (if Open Source)
Convince the 3rd party project to deploy the JAR with the class files to your Apache Archiva Repo (if Closed Source and in the same organization)
Convince the 3rd party project to deploy the JAR with the class files to a Maven Repository Manager of their own and register that repo in your Apache Archiva instance (if Closed Source and not in the same organization; not sure if Apache Archiva provides such a feature)
Build the JAR with the class files yourself and deploy it to your Apache Archiva repo. Then declare a dependency to it as usual

You can build the Jar file of the 3rd party source code and place it under you /lib folder and provide the dependency in your pom.xml file, Now when you will build your code the dependencies will be grab from the particular jar file.
<dependency>
<groupId>--grp id name--</groupId>
<artifactId>--artifact id--</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>--${basedir}\lib\{name}.jar--</systemPath> (Path of jar)
</dependency>
</dependencies>
*Remove "-- --" while implementing.

You can use the Build Helper Plugin, e.g:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>path to where your 3rd party source code is present</source>
...
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Related

Maven : How to package external jars(folder) in Maven project

I am having a group of external jars(in hundreds) which I have added in the build path of my project to make it work.
But while packaging it is failing as these jar's are not available to maven.
I have gone though many articles and all the solutions(like adding the jar at system path) are for a single jar only.
<dependency>
<groupId>com.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/Name_Your_JAR.jar</systemPath>
</dependency>
Is there any way we can add the group of jars(folder) to the packaging on the project? or any other solution by which my project can build successfully?
can we create a single jar containing all my jars inside and then use the system scope of maven
I have tried creating jar by jar -cvf my_jar.jar * and placed this jar in the system scope. But it does not worked for me.
My solution : Maven pluggin addjar let us add all jar at a place(projectdirectory/lib in this case).
this enables you to add these jar's in the final package(jar in my case) when you maven build, but to run locally you have to add those jar files directly in the classpath.
<plugin>
<groupId>com.googlecode.addjars-maven-plugin</groupId>
<artifactId>addjars-maven-plugin</artifactId>
<version>1.0.5</version>
<executions>
<execution>
<goals>
<goal>add-jars</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${basedir}/lib</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Now create a shade jar using mvn clean install shade:shade
The bad news: For a proper Maven project, you need to add each and every artifact you use as <dependency> in your POM.
The good news: I very much doubt that these 100 jars are all
- directly used in your source code
- not available in a public Maven repository like MavenCentral
So the better strategy would be to figure out what you really need and find that in MavenCentral. Then Maven finds all the transitive dependencies for you. So if you really need 10 of the jars and all other jars are just dependencies of your dependencies, just add these 10 ones (from MavenCentral) and you are done.

Maven:How to exclude all thirdparty jar from web project source code?

Currently we have a JSF Based web project which we are deploying in Jboss EAP 6.1 server.Its a multi-module project with plenty of third party jars added inside web-porject/lib folder.
When we run mvn install it create war file and we deploying this war file in Jboss but issue that its size is too big around 300MB.So sometime its hard for us deploy in war in the server if any single line changes in code its taking time upload the project in server.
We want we should separate out jar file(Third Party) and source code .So war size should be less and all third party jar should be seperate out from the source code.
This project is maven based project so all the dependency added in pom.xml file so for developer prospective we should not remove the pom file entry .
Can Someone please suggest what should be best way to handle this situation?
You should do that in two steps.
First exclude dependencies from packaging and use them only for compilation phase:
<dependencies>
<!-- declare the dependency to be set as optional -->
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>compile</scope>
<optional>true</optional> <!-- value will be true or false only -->
</dependency>
</dependencies>
In the second step put all the jars needed for running the application in jboss JBOSS_HOME/lib folder.
You can do that by using Copying project dependencies maven plugin:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

war project dependency in maven

I need to access one bean class from war project into my another war project. The bean class is exists in MyProject. I wrote pom of another project called NewProject as follows.
<groupId>MyProject</groupId>
<artifactId>MyProject</artifactId>
<version>1</version>
</parent>
<artifactId>MyProject</artifactId>
<packaging>war</packaging>
Is it possible to add war dependency in another war project?
If you configure the maven-war-plugin with the following attribute:
<attachClasses>true</attachClasses>
you would get an supplemental artifact with the following coordinates:
<dependency>
<groupId>myGroup</groupId>
<artifactId>myArtifact</artifactId>
<version>myVersion</myVersion>
<classifier>classes</classifier>
</dependency>
which contains all classes within your war project which can be used as dependency which is a jar file which will solve your problem.
In your war project
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration></plugin>
It creates a classes artifact which you can use in the required project
<dependency>
<groupId>your-group-id</groupId>
<artifactId>your-artifact-id</artifactId>
<version>your-version</version>
<classifier>classes</classifier>
refer maven war plugin
hope it helps...
Dependencies work using jars, and you would normally define a common dependency in a .jar file that can be accessed by both .wars. That's not Maven-specific, but how dependencies in Java work. The internal structure of a .war is different from a .jar in terms of how the classes are laid out.
Consequently in Maven, I would expect to define a .jar project, and then two .war projects both depending on the initial project.
You are better off IMHO creating a jar with your war classes that are needed in your project.
And then just add the dependency to your project configuration (classifier classes).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>classes</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>classes</classifier>
</configuration>
</execution>
</executions>
</plugin>
Move your re-usable classes into a separate module. This will help:
Test things quicker.
Use the code as a dependency in other projects.
Alternatively, you could produce a classes only jar by using the maven-jar-plugin and producing a classifier-based artifact. However, I think my suggestion is better in the fact that it give you a clear separation of the code and forces you to organize your code better.

Deploying Java project to Heroku without Maven-supported dependencies

Is anyone working with Heroku for Java?
I have one Java project which I want to deploy on Heroku. That project uses some external JAR files which contains important dependencies.
Can anyone tell me how to deploy my project with these JAR files to Heroku? Maven is not able to download these JAR files on Heroku.
We just published a guide showing how to add dependencies like this to your project:
http://devcenter.heroku.com/articles/local-maven-dependencies
Let me know if this works for you.
You need to setup a local Maven repository containing your jars. Include that repo in your git repo. And add the repo to the pom.xml file:
<repositories>
<repository>
<id>local-libs-dir</id>
<name>locallib</name>
<url>file:${project.basedir}/libs</url>
</repository>
</repositories>
The jar files must be in the standard Maven repo layout and have md5 & sha1 checksums.
You can use jcabi-heroku-maven-plugin, which automates the entire deployment process:
<plugin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-heroku-maven-plugin</artifactId>
<version>0.4.1</version>
<configuration>
<name>my-test-app</name>
<artifacts>
<artifact>com.example:example-app:jar::${project.version}</artifact>
</artifacts>
<procfile>web: java -Xmx256m -jar ./example-app.jar \${PORT}</procfile>
</configuration>
<executions>
<execution>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
Besides that, you have to deploy your artifact (JAR/WAR) to your repository, so that Maven inside Heroku can download it during deployment.

Is it best to Mavenize your project jar files or put them in WEB-INF/lib?

I've been doing this for all of the jar files that my Spring MVC project needs:
call mvn install:install-file -DgroupId=vegetables -DartifactId=potatoes -Dversion=1.0 -Dfile=vegetables-1.0.jar -Dpackaging=jar -DgeneratePom=true
Recently I must have exceeded some limit on how many dependencies you can list in your pom.xml file because I got an error that said:
Your command line is too long
So I removed some dependencies from the pom.xml that my project no longer uses and I was able to run the project with maven again.
My question is, should I put install all jar files into my Maven repository as I have been doing so far? Or should I put some of them into the WEB-INF/lib directory?
What's the best practice here?
I've been doing the same that you do with the command line, but by configuring maven-install-plugin in my POM (please read the note at the end):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>install-vegetables</id>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>${project.basedir}/lib/vegetables-1.0.jar</file>
<groupId>vegetables</groupId>
<artifactId>potatoes</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
</configuration>
</execution>
<execution>
<id>install-minerals</id>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>${project.basedir}/lib/minerals-1.0.jar</file>
<groupId>minerals</groupId>
<artifactId>rocks</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
</configuration>
</execution>
</executions>
</plugin>
It is much less efficient, because files get installed over and over, but it is much less annoying than making it manually. Anyway, I think you should give it a try.
All your dependencies should reside under the local repository. According to the Maven convention/best practices, you should not keep jar files in your project.
Convert your project to a fully war based Maven project. This will place all your dependencies (jar files) under your webapp's WEB-INF/lib directory. Thus you will not have to worry about long paths.
You just need to add the dependencies in your pom.xml file, no need to install them manually. Maven will download the libraries and put it in your local repository whenever needed. Only if you want to use third party(custom) libraries, you may go for installing it in your local repository.

Categories