-1

i might have completely missundestood how maven works, but i have the following issue. i created an application with maven in netbeans. it runs in netbeans fine, but when i make Clean and Build it just compiles my source files, and the maven dependendencies are not in the jar file, so cannot run in command window.

this is my pom file. it has build section, the dependencies are in the classpath of mainfest.mf

<?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>com.ibh</groupId>
<artifactId>SafePassword</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.ibh.safepassword.TestMain</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.192</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator-annotation-processor</artifactId>
        <version>4.2.0.Final</version>
        <type>jar</type>
    </dependency>
</dependencies>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

i came from c# world, thought maven is similar than NuGet which is playing the game only in DEV phase, but at compile time everything is there ...

what is missing? how to execute the jar file to have the other dependencies in place and so the app can run?

thanks for your hints!

EDIT

i don't agree that this question is fully duplicated to the one you're referring to. as i can read these many solutions maven is quite complicated, there is no ONE solution for every issue

pillesoft
  • 486
  • 1
  • 6
  • 20

1 Answers1

1

By default Maven builds a jar that is basically a library - it doesn't contain its dependencies. If you want to include dependencies, making a runnable application, you have two main options:

  1. Bundle everything in that one jar using some plugin that allows it, such as maven-shade-plugin. You can then run this jar.
  2. Create a distribution bundle with your jar and all dependencies:
    1. Use packaging that is designed to be a distribution, such as war or ear - if one of them matches your intent
    2. Otherwise use generic bundling plugin, such as standard maven-assembly-plugin or even more advanced appassembler-maven-plugin
Anton Koscejev
  • 4,603
  • 1
  • 21
  • 26