How to create a Maven Artifact, from lonely *.jar files? - eclipse

I'm using Eclipse to manage my SVN repository, and I have a few projects that depend on the same Jar files, I would like to create a maven artifact of these Jars (if possible using Eclipse) and commit that artifact into my SVN repository for further use.
How can I achieve that?
Thanks,
Adam.

Create an Eclipse project which contains all the JARs and an ANT build.xml which calls mvn file:install for each JAR to install then in your local Maven repository (see the docs for options of mvn file:install).
That way, you can use these JARs like any other Maven dependency after running the build.xml once.
[EDIT] A sample target would look like this:
<target name="maven-file-install">
<exec executable="mvn"> <!-- Make sure mvn is in the PATH -->
<arg value="file:install"/>
<arg value="-Dfile=your-artifact-1.0.jar"/>
... all the other arguments of file:install ...
</exec>
</target>

I suggest using a file based repository:
Create a module containing a file based repository
Install the jars in this file based repository
Commit the module to SVN
Detailed steps are provided in this previous answer.
Similar questions
Maven: add a dependency to a jar by relative path

mvn install:install-file did the trick:
Install-File

Related

Can maven treat WEB-INF\lib the way eclipse (and m2e) does?

I have a servlet/jsp web project which runs fine on eclipse and is exported as war fine (once I clean it that is). I mavenized the project deleting all of the dependencies from the WEB-INF\lib folder except a homebrew jar (the output of another project in the workspace). When I run the package maven goal I get messages for missing classes from this jar:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
#..... NOTICE THIS COMES FROM A CUSTOM JAR
[ERROR] /C:/path/DataServlet.java:[3,30] package xxx.java.helpers does not exist
Now this has been asked before and the most rigorous solution appears to be to create a local repo: Can I add jars to maven 2 build classpath without installing them? (NB: I am at maven 3).
I would like to avoid this - so is there any way maven will just stuff this jar to WEB-INF\lib in the war ?
Solutions that use some maven plugin to cp the contents of the WEB-INF\lib in the war are welcome - although I just have this feeling that there should be a solution that takes into account the "special" nature of this folder.
Observations:
Alt+F5 removes this line:
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
which corresponds to the "Web App libraries" in the Java Build Path. So not only maven refuses to take into account the WEB-INF\lib - it also breaks the build path of eclipse completely.
Related:
Maven: How to include jars in Eclipse, which are not available in repository?
Uses the maven eclipse plugin : update my classpath with an Eclipse User Library via the maven eclipse plugin - not compatible with m2e
How does the m2e eclipse plugin interact with eclipse? - apparently m2e checks the pom then calls the eclipse builders (hence the .classpath is read)
Eclipse maven-enabled web app references workspace projects, but those are not deployed when running Tomcat server
Deploying a Maven project with dependencies to Tomcat or Jboss running within Eclipse
Did you add this jar from WEB-INF\lib as a dependency like this:
<dependency>
<groupId>someGroupId</groupId>
<artifactId>someArtifactId</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/homebrew jar</systemPath>
</dependency>

Beginner's Guide to Setup Xuggler

To work with Xuggler you need xuggle-xuggler-5.4.jar. According to the people who made this, most users only need the above-mentioned JAR file. However, this is what they say about using Xuggler without Maven or Ivy:
Using Xuggler WITHOUT Apache Maven or Apache Ivy
What are you... stuck
in 2003? Anyway, if you insist on this, Xuggler's pre-compiled
binaries (including native versions) can be found here. Make sure that
xuggle-xuggler.jar and its dependencies are included in your Java
classpath. See the xuggle-xugger-*.pom file distributed with the
version of Xuggler that you use to find the (small) set of dependent
jars, and download them as well.
I downloaded the xuggle-xuggler.jar file for the latest version, 5.4 but I don't understand the pom file for it.
What dependencies is he talking about?
Next, how do I download these dependencies ?
Once I get these dependencies, how do I start working in Eclipse?
Update After Downloading Dependencies
I have the following directory structure:
xuggle-xuggler-5.4.jar is stored in E:\xuggle
the various xuggler dependencies are stored in E:\xuggle\xuggle-dependencies
Question:
How do I start working with Xuggler in Eclipse? What paths do I have to set and what values do these paths have?
The following files list the other jars which xuggle depends upon:
ivy.xml
pom.xml
You can read these and then manually retrieve them from the appropriate repository, but I would submit it's simpler to start using a dependency manager.
You asked how to download these dependencies, well ivy has a convenient command-line mode of operation. (See example below)
Eclipse integration is very tough.... Once you've downloaded the jar you could try and generate the ".classpath" file or just manually add each jar via the Eclipse GUI.
The reason I don't recommend this approach is because there are Eclipse plugins for both Maven and Ivy that would do this for you automatically.
Example
Run ivy from command-line as follows:
java -jar ivy.jar -settings ivysettings.xml -dependency xuggle xuggle-xuggler 5.4 -retrieve "lib/[artifact]-[revision].[ext]"
It will retrieve xuggle and all its dependencies into a "lib" directory as follows:
├── ivysettings.xml
└── lib
├── commons-cli-1.1.jar
├── logback-classic-1.0.0.jar
├── logback-core-1.0.0.jar
├── slf4j-api-1.6.4.jar
└── xuggle-xuggler-5.4.jar
ivysettings.xml
This file tells ivy to retrieve jars from either Maven Central, or the Maven repository provided by the Xuggle project.
<ivysettings>
<settings defaultResolver="repos" />
<resolvers>
<chain name="repos">
<ibiblio name="central" m2compatible="true"/>
<ibiblio name="xuggle" m2compatible="true" root="http://xuggle.googlecode.com/svn/trunk/repo/share/java"/>
</chain>
</resolvers>
</ivysettings>
Don't fight Maven, embrace it. These days all major build systems are maven compatible (Maven, Ivy, Gradle, Grape, Buildr ...). But you can use Maven from Eclipse:
create a file called pom.xml with this content:
<project>
<groupId>com.foo<groupId> <!-- change these -->
<artifactId>foo</artifactId> <!-- parameters to whatever -->
<version>1.0-SNAPSHOT</version><!-- you like -->
<repositories>
<repository>
<id>xuggle repo</id>
<url>http://xuggle.googlecode.com/svn/trunk/repo/share/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>xuggle</groupId>
<artifactId>xuggle-xuggler</artifactId>
<version>5.2</version>
</dependency>
</dependencies>
</project>
Install the m2e extension and, from Eclipse, do "File > Import ... > Existing Maven Projects". In the dialog, select the Folder that contains the pom.xml.
Make sure that the Folder's layout is like this:
pom.xml
src/main/java // sources go here
src/test/java // test sources go here
Then you should have a working Eclipse project with the required dependencies.
Update after your update:
You can see the dependencies when you look at this file: http://xuggle.googlecode.com/svn/trunk/repo/share/java/xuggle/xuggle-xuggler/5.2/xuggle-xuggler-5.2.pom
commons-cli (a utility library for command line processing)
logback (a logging framework)
junit (a testingframework)
Maven will take care of loading these dependencies for you. So will Eclipse, if you use the m2e plugin as suggested above.
If you absolutely don't want to do that, you will have to download the dependencies manually. Look at the pom file above, note the names and versions of the dependencies, look them up at http://mvnrepository.com/ and download them there, e.g. this is the page for slf4j-api: http://mvnrepository.com/artifact/org.slf4j/slf4j-api/1.6.4

Eclipse Maven Dependency

I have just added dependencies to an eclipse project so that my jar can see other jars. How can I be sure that the dependencies work, and that what I've done is correct? I view a pom.xml file that has been created so what are the target folder and classes,test-classes subfolders used for? Thanks
If you have the m2eclipse plugin installed you can open your pom in Eclipse and click on the Dependency Hierarchy tab to view your resolved dependencies. You should manage all dependencies through Maven with the setup you are describing.
If you want to check command line you may want to look at using Effective Pom.
If you use m2e, it adds Maven Dependencies pseudo library to your project.
You may expand it and see if the dependent jar file is in there.
If it is, Eclipse ( or more precisely m2e ) has resolved the dependency correctly and it's available for you project build.
If you added your dependencies correctly your application should build and execute correctly, or am I missing something? Dependencies should be added to a POM section that looks like this example:
<dependencies>
<dependency>
<groupId>annogen</groupId>
<artifactId>annogen</artifactId>
<version>0.1.0</version>
</dependency>
<!-- other dependencies here -->
</dependencies>
Maven and the m2e/m2eclipse plugin rely on source files to be conventionally placed in src/main/java for application code and src/test/java for test code. Application code is compiled to target/classes and test code is compiled to target/test-classes. If you plan to use Maven and/or m2e/m2eclipse, do read about it. Maven: The Complete Reference is a good starting point.

Eclipse Project with Dependency Management by Maven

I have a Eclipse project where Maven manages the dependencies. I have also few jar files that are not Maven enable and I locate them at src/main/webapp/WEB-INF/lib. I have no issue to build/run the project in Eclipse. I have no issue also to run "mvn:package" after I built the project in Eclipse. However, after I invoke "mvn:clean", if I run "mvn:package", I will get compilation error as it can't find dependency jar files under src/main/webapp/WEB-INF/lib. What I need to do is to rebuild the Eclipse project then "mvn:package". Therefore, I can't invoke "mvn:package" outside Eclipse IDE.
How to resolve this?
Thanks.
You have to put the not "Maven enabled artifacts" to an appropriate Maven Repository (Nexus, Artifactory what ever) and than change your project to use the dependencies appropriately. Furthermore either you do Maven or not but nothing in between. Maven is a build tool and not only for dependency management. After those changes working with Eclipse will work fine (if you use M2Eclipse). If you correctly use Maven you can do both things via Eclipse or call mvn package on command line.
If you can not set up a recommended environment (maven repository) you can add the dependencies as System dependencies to your pom.xml.
<project>
...
<dependencies>
<dependency>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.2.3</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/foobar.jar</systemPath>
</dependency>
</dependencies>
...
</project>

Modify project build path with Apache Ant in Eclipse

I have an ant script that does the tipically: clean, compile, jar, javadocs, etc. I also have 3 projects in Eclipse: a library and 2 projects to test it.
In the build path of this 2 test projects I've defined as external jar the library jar located in the library project.
The library jar has its version in the jar name, i.e. library-0.1.jar. In the ant script I have a property with the version of the library:
<property name="project_version" value="0.1"/>
So to change the version I modify this property and run the script again. As you may deduce this generates a dependency error in the 2 other projects because they will still be pointing to an old file library-0.1.jar.
How can I change automatically the build path of that 2 other projects in Eclipse? Apache ant can do this with a specific tag?
Thanks.
Refer to the version with a variable in all your build files, e.g.
<include name="my-${version}.jar"/>
Now when you execute your builds, you can execute with explict version to match what you require, e.g.
ant -Dversion=1.3
Alternatively, you could load the same properties file in each of your build scripts to load the version property
<property file="version.properties">
Note that if you go with the latter you should remove the property declaration (from you post above) which sets the value explicitly. Either that or load the properties file first.
....
Use sed, e.g. (not tested):
version=2
sed s/value="\d+"/value="$version"/g build.xml
EDIT: Using the method below you will be able to compile using Ant, but eclipse will show you a dependency error in the project explorer because you don't have defined any external jar in the build path panel. To solve this you have to edit the .classpath file that you will see in the project root and add the following line:
<classpathentry kind="lib" path="../Library/bin"/>
Where Library is the folder for Library project and bin the folder for classes.
SOLVED:
I have to write an ant script for the 2 other projects and set the classpath with the script, not with eclipse IDE:
<path id="build-classpath">
<fileset dir="${dist}">
<include name="${project_name}-${project_version}.jar"/>
</fileset>
</path>
${dist} is the folder where is the jar library it's something like: "../Library/dist", where Library is the name of the project.
${project_name} and ${project_version} are loaded from a version.properties file that, again, is stored in "../Library":
<property file="..Library/version.properties"/>
The file version.properties just contains:
project_name=LibraryName
project_version=0.1
Then, to add the classpath when compiling...
<target name="compile" depends="clean, makedir">
<javac srcdir="${src}" destdir="${bin}">
<classpath refid="build-classpath"/>
</javac>
</target>
The refid value is the path id defined previously.