Building jar with dependencies maven

Daan Luttik

I can't find a comprehencive guide to how maven building actually works. And trying to follow the advice I find at stackoverflow I wound up with a monster sized maven build branch that still doesn't work. What I wan't is a minimalistic maven build protocol that includes all dependencies and simply just runs.

This is the broken maven file that I have now.

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>image-converter</groupId>
    <artifactId>image-converter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20141113</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>image-converter-${project.version}</finalName>
        <directory>../</directory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>${project.build.directory}/lib</classpathPrefix>
                            <mainClass>com.luttikDevelopment.imageConverter.Converter</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
mszalbach

The easiest thing to build is an executable uper jar which contains all dependencies and can be used via:

java -jar xxx.jar

This can be archived with the maven-shade-plugin

So your pom.xml would look like this:

 <?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>image-converter</groupId>
<artifactId>image-converter</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20141113</version>
    </dependency>
</dependencies>

<build>
    <finalName>image-converter-${project.version}</finalName>
    <plugins>                       
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.1</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                    
            <mainClass>com.luttikDevelopment.imageConverter.Converter</mainClass>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>
    </plugins>
</build>

So you can build this with maven package and find the uber jar in the target folder.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Maven building a jar with dependencies unpacked inside the JAR

From Dev

Maven building jar: merge duplicate resources from dependencies

From Java

Building executable jar with maven?

From Java

Maven jar with dependencies?

From Java

Including dependencies in a jar with Maven

From Java

Building a runnable jar with Maven 2

From

Building a fat jar using maven

From Java

Maven failing to download jar dependencies

From Dev

Maven shade plugin - Jar and dependencies

From Dev

Maven not adding all dependencies to jar

From Java

Maven deploy jar with dependencies to repo

From Java

Maven: Add local dependencies to jar

From Dev

The maven dependencies are not added to the jar in eclipse

From Dev

Maven jar without dependencies by default

From Dev

jar download with dependencies from maven

From Dev

Building/deploying a EJB .jar with its dependencies

From Java

Building JAR that includes all its dependencies

From Dev

ant to maven migration maven - jar building

From Dev

Maven building successfully without actually downloading dependencies?

From Java

Maven jar dependencies are displayed outside `Maven Dependencies` view

From Java

jar file gets corrupted while building with maven

From Java

Building an executable jar with maven depencies and external jars

From Dev

Maven building a project using both Maven Dependencies and Referenced Libraries

From Java

How To: Eclipse Maven install build jar with dependencies

From Java

Generate jar file in Maven with dependencies and tests

From Dev

Maven copy dependencies with full jar name

From Java

Export maven as jar file with all dependencies

From Dev

Maven dependency plugin copy jar with dependencies

From Dev

Maven - How to exclude dependencies from generated JAR

Related Related

  1. 1

    Maven building a jar with dependencies unpacked inside the JAR

  2. 2

    Maven building jar: merge duplicate resources from dependencies

  3. 3

    Building executable jar with maven?

  4. 4

    Maven jar with dependencies?

  5. 5

    Including dependencies in a jar with Maven

  6. 6

    Building a runnable jar with Maven 2

  7. 7

    Building a fat jar using maven

  8. 8

    Maven failing to download jar dependencies

  9. 9

    Maven shade plugin - Jar and dependencies

  10. 10

    Maven not adding all dependencies to jar

  11. 11

    Maven deploy jar with dependencies to repo

  12. 12

    Maven: Add local dependencies to jar

  13. 13

    The maven dependencies are not added to the jar in eclipse

  14. 14

    Maven jar without dependencies by default

  15. 15

    jar download with dependencies from maven

  16. 16

    Building/deploying a EJB .jar with its dependencies

  17. 17

    Building JAR that includes all its dependencies

  18. 18

    ant to maven migration maven - jar building

  19. 19

    Maven building successfully without actually downloading dependencies?

  20. 20

    Maven jar dependencies are displayed outside `Maven Dependencies` view

  21. 21

    jar file gets corrupted while building with maven

  22. 22

    Building an executable jar with maven depencies and external jars

  23. 23

    Maven building a project using both Maven Dependencies and Referenced Libraries

  24. 24

    How To: Eclipse Maven install build jar with dependencies

  25. 25

    Generate jar file in Maven with dependencies and tests

  26. 26

    Maven copy dependencies with full jar name

  27. 27

    Export maven as jar file with all dependencies

  28. 28

    Maven dependency plugin copy jar with dependencies

  29. 29

    Maven - How to exclude dependencies from generated JAR

HotTag

Archive