Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven-assembly-plugin doesn't add dependencies with system scope

Tags:

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?

like image 487
Macarse Avatar asked Apr 06 '10 21:04

Macarse


People also ask

Where do you put dependencies in POM xml?

Open the pom. xml file. under the project tag add <dependencies> as another tag, and google for the Maven dependencies.

What is the difference between plugins and dependencies in Maven?

A plugin is an extension to Maven, something used to produce your artifact (maven-jar-plugin for an example, is used to, you guess it, make a jar out of your compiled classes and resources). A dependency is a library that is needed by the application you are building, at compile and/or test and/or runtime time.

What is Dependencymanagement in POM xml?

The dependency management section is a mechanism for centralizing dependency information. When you have a set of projects that inherit from a common parent, it's possible to put all information about the dependency in the common POM and have simpler references to the artifacts in the child POMs.

What is scope in Maven dependency?

Maven defines 6 scopes: compile, runtime, provided, system, test, and import. Maven defines the behavior for each scope as following (copied verbatim from the dependency management page)


2 Answers

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.

like image 105
Pascal Thivent Avatar answered Sep 22 '22 11:09

Pascal Thivent


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> 
like image 22
Othmen Tiliouine Avatar answered Sep 25 '22 11:09

Othmen Tiliouine