views:

354

answers:

4

I'm mavenizing some projects.

These projects all depend on a number of libraries, most of them are available in the maven repo. For the other libraries, I'd like to create a maven artifact, so I can use it as an dependency. The problem is, I only have jar files of these libraries.

What is the best way to create artifacts from existing jar files?

thx

-- 3wh

+2  A: 

You can use the Maven Deploy Plugin to upload your JAR file (and optionally a POM file, though by default one will be created for you) to your Maven repository.

danben
+1  A: 

As danben said, you'll have to deploy these jar to a repository. However, I seem to understand from your question that you don't have a repository except the global maven one.

You could use Nexus, or Artifactory.

Valentin Rocher
A: 

If you're not using a remote repository (which is a common situation for personal development), simply install these artifacts in your local repository using the install:install-file mojo:

mvn install:install-file 
  -Dfile=<path-to-file> 
  -DgroupId=<group-id> 
  -DartifactId=<artifact-id> 
  -Dversion=<version> 
  -Dpackaging=<packaging> 
  -DgeneratePom=true

Where: <path-to-file>  the path to the file to load
       <group-id>      the group that the file should be registered under
       <artifact-id>   the artifact name for the file
       <version>       the version of the file
       <packaging>     the packaging of the file e.g. jar

But obviously, this will make your build non portable (this might not be an issue though). To not sacrifice the portability, you'll have to make the artifacts available in a remote repository. In a corporate context, the common way to deal with that is to install an enterprise repository (and in that case, to deploy the artifacts indeed).

Update: Once your artifact is installed in your local repository, simply declare a <dependency> element in your pom like for any other dependency, e.g.:

<dependency>
  <groupId>aGroupId</groupId>
  <artifactId>aArtifactId</artifactId>
  <version>1.0.12a</version>
  <packaging>jar</packaging>
</dependency>
Pascal Thivent
Same question as above, does install-file resolves dependencies? Or does it only upload the jar file into local repository? If second case, how would you create the dependency entries in your pom.xml?
Nils Schmidt
@Nils No, install:install-file won't resolve dependencies. How could this happen? Maven doesn't have any knowledge of these artifacts and their dependencies if they are not "mavenized". So, indeed, it will only upload the specified jar file. And I'll update my answer to show you how to declare dependency in your pom.xml.
Pascal Thivent
Okay, that was my guess. As manuel describes, dealing with transitive dependencies in a manual way is painful. So my thought (and I thought this reflected somehow 3wh question) was how to deal with the dependencies when mavenizing a jar.I remember that one of my former colleauges had written some scripts that looked into a jar, its sources/class files and the lib folder, and with that information tried to create a pom with dependencies as good as possible. But as it seems, this thing never made into the maven plugin repository. Or is there something similar?
Nils Schmidt
A: 

I know your problem. Mavenizing jars is sometimes a pain (especially if they have further transitive dependencies, which also need to be defined in pom.xml).

Have you checked whether these libraries really never exist as maven deps? Have a look at the usual suspects:

Sometimes I like to use Nexus jar upload dialog to let create pom.xml files.

manuel aldana
Don't look manually, use a search engine: http://maven.apache.org/general.html#How_to_find_dependencies
Pascal Thivent
you're right. that is the first step to do. still (though very rare) I sometimes had to search manually, because search engines couldn't find the dependency.
manuel aldana