Tag Archives: Maven

Maven: Set name of final packaged artifacts

Here’s how to set the final name of the artifact generated by Maven.

<build>
    <!-- Sets name of regular artifact -->
    <finalName>${project.artifactId}</finalName>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <!-- Name of assembled jar with all dependencies -->
                <finalName>${project.artifactId}</finalName>
                <!-- Set to false to prevent '-jar-with-dependencies' to be appended to the file name -->
                <appendAssemblyId>true</appendAssemblyId>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Note that we use a variable here so that the name will be the same as the artifactId. It could of course also be just a regular hard coded string. Also note that since we’re using the same name for both packaged artifacts, the latter (the one with dependencies) will overwrite the first.

Maven: Add 3rd party dependencies in project specific repository

Sometimes we need to use some commercial artifacts which are not available in the usual Maven repositories. These should of course be added to a repository hosted by your company or something like that, but sometimes it’s just simpler to stick those jars in the project directly and check them in with the rest of the code.

Found a nice way of doing that in the heroku devcenter documentation, so noting it down here for future reference.

Create repository

We’ll start with deploying an artifact to a repo folder in our project by using the following command. I split it up so it’s easier to read, but it should of course all be a single command.

mvn deploy:deploy-file
    -Durl=file:///dev/project/repo/
    -Dfile=somelib-1.0.jar
    -DgroupId=com.example
    -DartifactId=somelib
    -Dpackaging=jar
    -Dversion=1.0

The group and artifact id you’d have to make up yourself of course. For example pull the artifact id from the library name and the group id from the main package used inside the library.

Remember to add this folder to your version control.

Define repository

Add the following to your pom.xml to let Maven know about the project repository we just created.

<repositories>
    <repository>
        <id>project.local</id>
        <name>project</name>
        <url>file:${project.basedir}/repo</url>
    </repository>
</repositories>

Define dependency

And finally just define the dependency in your pom.xml like you’d usually do.

<dependency>
    <groupId>com.example</groupId>
    <artifactId>somelib</artifactId>
    <version>1.0</version>
</dependency>

Clean and simple.

Maven: Package runtime dependencies in lib folder inside packaged jar

When running mvn package, here’s how to end up with a lib folder inside the resulting jar file which contains all the runtime dependencies of your project. Just add the following to your pom.xml.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Works on my machine anyways. When I now open up the packaged jar archive I find a lib folder containing all jar dependencies, with the exception of those belonging to the scopes test and provided. Which is what I wanted. Score!!

Maven: Sources and javadocs for Eclipse

Had a Maven project here which used Hibernate for database stuff. I’m kind of a newb when it comes to Hibernate and other Java libraries so I rely quite a bit on learning and discovery through the auto-completion and inline documentation in Eclipse. Problem was that when I edited this project, this auto-completion and documentation was very unhelpful. No documentation at all and parameter names of methods were just series of arg0, arg1, and so on. Quite annoying.

Download sources and javadocs

Turns out you need to tell maven to download this stuff in the pom.xml.

    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    ...
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                    ...
                </configuration>
            </plugin>
            ...
        </plugins>

Then you just need to rebuild the Eclipse project files.

> mvn eclipse:clean eclipse:eclipse

After a refresh of the project in Eclipse you should now have a much more helpful auto-complete and inline java documentation 🙂

Generate sources and javadocs

Now if you have your own Maven repository with your own artifacts, the above steps will solve nothing since there (by default) won’t be any sources or javadocs to download. For your own artifacts that is. This requires telling Maven to actually generate these things for your artifact and then uploading those together with the regular jar file. This is how you do that:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>2.2.1</version>
    <executions>
        <execution>
            <id>attach-sources</id>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>2.9</version>
    <executions>
        <execution>
            <id>attach-javadocs</id>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

When you now run mvn package you should get your regular artifact and two more. One with the sources and one with the javadocs. Upload the regular artifact and attach those other two as well. When you then use the downloading config thing I mentioned first, you should now get javadocs and sources for your own artifacts as well.