25

This is my pom file:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sia.g7</groupId>
  <artifactId>sia</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>sia</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
     </dependency>
<dependency>
    <groupId>commons-math</groupId>
    <artifactId>commons-math</artifactId>
    <version>1.2</version>
</dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>jmathplot</groupId>
      <artifactId>jmathplot</artifactId>
      <version>1.0</version>
      <scope>system</scope>
      <systemPath>${project.basedir}/lib/jmathplot.jar</systemPath>
    </dependency>

    <dependency>
      <groupId>jgraphx</groupId>
      <artifactId>jgraphx</artifactId>
      <version>1.0</version>
      <scope>system</scope>
      <systemPath>${project.basedir}/lib/jgraphx.jar</systemPath>
    </dependency>

  </dependencies>
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.0.2</version>
      <configuration>
        <source>1.6</source>
        <target>1.6</target>
      </configuration>
    </plugin>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <appendAssemblyId>false</appendAssemblyId>
        <archive>
          <manifest>
            <mainClass>com.sia.g7.AbstractSimulation</mainClass>
          </manifest>
        </archive>
      </configuration>

    </plugin>
  </plugins>

</build>
</project>

And when I run the jar I get:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mxgraph/swing/mxGraphComponent

which is part of the jgraphx dependency. What am I missing?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Macarse
  • 91,829
  • 44
  • 175
  • 230

6 Answers6

18

you can do it by adding this dependencySet to your assembly file descriptor

<dependencySet>
    <outputDirectory>/</outputDirectory>
    <unpack>true</unpack>
    <scope>system</scope>
</dependencySet>

this assembly descriptor add all dependencies including system scoped

<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>jar-with-all-dependencies</id>
<formats>
    <format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <useProjectArtifact>true</useProjectArtifact>
        <unpack>true</unpack>
        <scope>runtime</scope>
    </dependencySet>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <unpack>true</unpack>
        <scope>system</scope>
    </dependencySet>
</dependencySets>

 </assembly>
  • 1
    this is working nicely, you have to reference the above `assembly.xml` file in the assembly-plugin configuration though via ` false ${basedir}/assembly.xml ` – hotzen Feb 27 '20 at 19:28
15

Yes, and this is one of the reasons you shouldn't abuse system scope dependencies (which is globally a bad practice) and this problem has already been mentioned several times here on SO (here, here). I'm proposing a solution to deal with project relative dependencies in a "clean" way in this answer.

Community
  • 1
  • 1
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
2

You should remove the <scope>system</scope> clauses from those dependencies. When the scope is set to system that means the artifact is ALWAYS available, so jar-with-dependencies does not include it.

bruceberk
  • 29
  • 1
1

It must not be system scope in the first place. This is the source of your problem. Install/deploy your dependency into the repository and make it a normal compile (or runtime) scope dependency.

lexicore
  • 42,748
  • 17
  • 132
  • 221
0

There are 2 solutions:

  1. Recommended: create a repository and then provide the dependency in the same way as you provide dependency from maven repository.

  2. Though the above solution is the recommended one, but if you don't want to create the repository, then here is the workaround:

    1. First in your pom.xml, remove the system scope and the systemPath. Please note that the commented lines were removed to resolve this issue.

       <dependency>
       <groupId>com.paymentus.timgroup.statsd</groupId>
       <artifactId>paymentus-timgroup-statsd-client</artifactId>
       <version>1.0-SNAPSHOT</version>
       <!--<scope>system</scope>-->
       <!--<systemPath>${basedir}/external_lib/xyz.jar</systemPath>-->
       </dependency>
      
    2. Use maven-install-plugin in the same pom.xml. You need to modify the details in bold, no need to change any other thing in this plugin:

       <project> ....
       <profiles>...<plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-install-plugin</artifactId>
               <version>2.5.2</version>
               <executions>
                   <execution>
                       <id>install-external</id>
                       <phase>clean</phase>
                       <configuration>
                           **<file>${basedir}/external_lib/xyz.jar</file>**
                           <repositoryLayout>default</repositoryLayout>
                           **<groupId>com.my.project</groupId>**
                           **<artifactId>my-project</artifactId>**
                           **<version>1.0-SNAPSHOT</version>**
                           <packaging>jar</packaging>
                           <generatePom>true</generatePom>
                       </configuration>
                       <goals>
                           <goal>install-file</goal>
                       </goals>
                   </execution>
               </executions>
           </plugin>
       </plugins></profiles>
       </project>
      
utkarsh
  • 211
  • 2
  • 3
-4

Unpack your jar and add it to src/main/resources.