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 Dev

Maven not adding all dependencies to jar

From Dev

Using Maven in Ant for jar dependencies

From Dev

executable jar with dependencies using maven

From Dev

Maven generate jar dependencies then war

From Dev

Maven jar without dependencies by default

From Dev

Building/deploying a EJB .jar with its dependencies

From Dev

ant to maven migration maven - jar building

From Dev

Maven jar dependencies are displayed outside `Maven Dependencies` view

From Dev

Maven building a project using both Maven Dependencies and Referenced Libraries

From Dev

How to include maven dependencies in a jar file?

From Dev

maven: including selected dependencies for single jar

From Dev

Maven dependencies regarding javax.persistence JAR?

From Dev

Maven dependency plugin copy jar with dependencies

From Dev

Maven packaging project as executuble JAR with dependencies

From Dev

Intellij Maven: Create jar with all library dependencies

From Dev

what are the jboss-client.jar maven dependencies?

From Dev

Generate jar file in Maven with dependencies and tests

From Dev

remove all dependencies of a jar within a maven assembly

From Dev

Maven - How to exclude dependencies from generated JAR

From Dev

Maven: How to have jar-with-dependencies exclude "provided" dependencies?

From Dev

Combine java code and dependencies into one jar (executable jar) using maven

From Dev

How to package a jar and all dependencies within a new jar with maven

From Dev

Building runnable jar using onejar-maven-plugin

From Dev

Maven project packaging is war but also building/installing jar

From Dev

Maven: How to install my jar to local repo after building

From Dev

floreant POS : error in building jar file using maven

From Dev

Building C++ and Java code using CMake and Maven and bundle in a 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

    Maven not adding all dependencies to jar

  4. 4

    Using Maven in Ant for jar dependencies

  5. 5

    executable jar with dependencies using maven

  6. 6

    Maven generate jar dependencies then war

  7. 7

    Maven jar without dependencies by default

  8. 8

    Building/deploying a EJB .jar with its dependencies

  9. 9

    ant to maven migration maven - jar building

  10. 10

    Maven jar dependencies are displayed outside `Maven Dependencies` view

  11. 11

    Maven building a project using both Maven Dependencies and Referenced Libraries

  12. 12

    How to include maven dependencies in a jar file?

  13. 13

    maven: including selected dependencies for single jar

  14. 14

    Maven dependencies regarding javax.persistence JAR?

  15. 15

    Maven dependency plugin copy jar with dependencies

  16. 16

    Maven packaging project as executuble JAR with dependencies

  17. 17

    Intellij Maven: Create jar with all library dependencies

  18. 18

    what are the jboss-client.jar maven dependencies?

  19. 19

    Generate jar file in Maven with dependencies and tests

  20. 20

    remove all dependencies of a jar within a maven assembly

  21. 21

    Maven - How to exclude dependencies from generated JAR

  22. 22

    Maven: How to have jar-with-dependencies exclude "provided" dependencies?

  23. 23

    Combine java code and dependencies into one jar (executable jar) using maven

  24. 24

    How to package a jar and all dependencies within a new jar with maven

  25. 25

    Building runnable jar using onejar-maven-plugin

  26. 26

    Maven project packaging is war but also building/installing jar

  27. 27

    Maven: How to install my jar to local repo after building

  28. 28

    floreant POS : error in building jar file using maven

  29. 29

    Building C++ and Java code using CMake and Maven and bundle in a jar

HotTag

Archive