Skip to main content

Deploying non-Maven jars to java.net m2 repository

Posted by kohsuke on July 30, 2008 at 12:42 AM PDT

One of the problems I often have to help within Sun is to push jars to the Maven repositories. Many Sun projects, especially older ones, are normally built by Ant, so simplifying the deployment of those jars has been a challenge. And this is not just a problem for me alone — often when you are using Maven, you have dependencies that do not exist in Maven repositories yet, then you have to push those jars to the Maven repositories by yourself.

The "usual" way of doing this is to use "mvn deploy:deploy-file", but in my opinion this doesn't work very well for several reason:

  1. Using non-standard transport for deployment (in maven jargon, using custom wagon implementation) isn't very easy.

  2. If you intend to keep track of newer versons in the future, you need a wrapper shell script to automate this, and a shell script isn't portable nor easily automatable from Java.

In short, it's just not easy enough. And that's why I'm writing this post. In this post, I'll show you how you can do this by just writing one POM, which you basically just copy&paste from here with a few adjustments. You then run "mvn install" and it will push the jars and associated artifacts (if any) to the java.net Maven2 repository.

This technique combines wagon-svn and maven-antrun-extended-plugin, and here is the typical POM:

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
" title="http://maven.apache.org/maven-v4_0_0.xsd">
">http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>net.java.dev.jna</groupId>
  <artifactId>jna</artifactId>
  <packaging>jar</packaging>
  <version>3.0.3-patch-1</version>
  <name>Java Native Access</name>

   
   <distributionManagement>
    <repository>
      <id>java.net-m2-repository</id>
      <url>java-net:/maven2-repository/trunk/repository/</url>
    </repository>
  </distributionManagement>

  <build>
    <plugins>
      <!-- fake out maven and install the binary artifact -->
      <plugin>
        <groupId>org.jvnet.maven-antrun-extended-plugin</groupId>
        <artifactId>maven-antrun-extended-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <attachArtifact file="jna.jar" />
                <ant dir="." target="src-zip" />
                <attachArtifact file="build-d64/jna-src.zip" classifier="sources" type="jar"/>

              </tasks>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
    <extensions>
      <extension>
        <groupId>org.jvnet.wagon-svn</groupId>
        <artifactId>wagon-svn</artifactId>
        <version>1.8</version>
      </extension>
    </extensions>
  </build>
 
  <repositories>
    <repository>
      <id>maven2-repository.dev.java.net</id>
      <name>Java.net Repository for Maven</name>
      <url>http://download.java.net/maven/2/</url>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>maven2-repository.dev.java.net</id>
      <name>Java.net Repository for Maven</name>
      <url>http://download.java.net/maven/2/</url>
    </pluginRepository>
  </pluginRepositories>
</project>

The text in bold is the part that you need to modify. The first part should be hopefully obvious. These are the key parameters that identify the artifact in the Maven world. The group Id normally comes from your Java package name, artifact ID normally comes from your jar file name. The version and the name should be obvious.

The second part is a little Ant build script that tells Maven what files to deploy. If you are picking up jars that are built elsewhere, all you need is the tasks. You can see two variations of the task above that attaches the main jar and the source zip. If it takes a bit of massaging to build them, you can add more Ant tasks here to get the job done. In the above example, I'm really calling into Ant to build the source zip.

Oh, and to do this, you need to have the repository publisher role in the Maven2 repository java.net project, and a file that contains your credential to deploy files to the repository.

Related Topics >>

Comments

com4j is already available in http://maven.dyndns.org/2/org/jvnet/com4j/

And believe it or not, org.jvnet.PROJECT is the "correct" package name for java.net projects (even though there are many projects that don't follow the convention.) Powers-that-be felt that the use of "java" in the package name "net.java" was too confusing.

Hello kohsuke, Could you put your com4j librairie in http://download.java.net/maven/2/ directory ? May be your package name is not "standard" (net.java.com4j may be more correct) I use com4j to interface HP/Mercury TestDirector COM API with Eclipse Mylyn connector. I see all defects and attachments files store in TestDirector in Eclipse Mylin. And i use maven, hudson (maven, checkstyle, pmd, violation, disk usage), artifactory for others projects. Thank. Vincent

You probably just run the mvn install. That is a great first step to make sure your artifacts are good, but to actually deploy this to java.net, you need to do mvn deploy.

Hi kohsuke, I've tried your example pom.xml, but it installs in a local repository ($HOME/.m2/repository) as opposed to java.net. Perhaps I'm missing a maven plugin that parses a "java-net:/" URL (I'm using mvn 2.0.6 distributed with OSX).

cvidal -- Thanks for the pointer to Mavenizer. It's an interesting project (and I noticed that it's using JAXB, an extra plus point for me!.) In case of JNA, this library really doesn't have any dependencies, but the approach outlined in my posting obviously lets you define dependencies or any other POM entries easily.

coding -- Yes, if the audience of the artifacts are limited to your intranet, a repository manager would make a lot of sense.

For our environment we use Archiva to host our Maven repository. The latest version just released a few days ago allows web-based uploads. Before that I wrote a swing GUI that lets you drag-drop a jar to a text-field, enter a group, artifact and version. Finally it lets you enter a username and password (although Archiva does have a way to allow guests deploy too, which for now we have enabled (intranet), but as Maven gets used more and more at our company, we may disable that. When you fill everything out, it actually creates a POM on the filesystem and calls mvn to do the deploy-file goal. It worked pretty well.

Hi Kohsuke, Your advice is very interesting indeed. The problem I see is that the library you're mavenizing most likely has dependencies which might or might not exist in Maven repositories yet as well. And Maven poms have real value when those transitive dependencies are properly defined but getting those straight can be really cumbersome. In order to ease that process, you might want to give a look at Mavenizer: http://mavenizer.sourceforge.net/ It's a command line tool which assists with mavenizing those third party libraries and generating good quality maven metadata. There's a flash demo which shows the mavenization of jfreereport: http://mavenizer.sourceforge.net/demos/jfreereport-0.8.7.html Kind regards, Cédric Vidal