0

I'm learning Maven. I was tinkering with it and was struck at this issue,

I've two maven projects Project_1 and Project_2

Project_1 Sample code :

package com.sample;

public class App 
{
public void hello(){

    System.out.println("Hello from New Project1");

}
public static void main( String[] args )
    {
        //Empty
    }
}

I build project_1 and added the JAR file as dependency to project 2.

Project_2 Sample code :

package com.newsample;
import com.sample.App;

public class Appn 
{
    public static void main( String[] args )
    {
        App obj=new App();
    obj.hello();
    }
}

Project_2 POM.xml dependency :

<dependency>
      <groupId>com.sample</groupId>
      <artifactId>project1</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

However, When I try to run the package from the JAR file, it is throwing below error :

Exception in thread "main" java.lang.NoClassDefFoundError: com/sample/App

I've checked the project_2 JAR file but App.class was not added to it. My question seems to be naive, but shouldn't the class be added since it is a dependency for project_2. Is there any other way to bundle this using maven?

2 Answers2

1

Use the shade plugin to produce a runnable jar (a jar with all it's dependency jars exploded).

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>com.....Application</Main-Class>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
Essex Boy
  • 7,565
  • 2
  • 21
  • 24
0

Dependencies are not included in the build of a jar artifact.

Possible duplicate of How can I create an executable JAR with dependencies using Maven?

spi
  • 1,673
  • 13
  • 19
  • 1
    I think the shade plugin is much easier to use than the assembly plugin for this. – Essex Boy Jun 23 '17 at 12:26
  • why not? as ever with java, it's just one of the vast number of options available. – spi Jun 23 '17 at 12:37
  • If you believe this question is a duplicate, please **[flag it as such as described here](https://stackoverflow.com/help/privileges/flag-posts)**. Thanks. – Pang Jun 24 '17 at 01:14