I'm quite new in Maven.
I'm trying to do following:
Suppose we have to projects A and B. Project B needs to use some classfrom_A from jar imported from A
Here are definitions :
POM.xml of project A :
<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>A_Group</groupId>
<artifactId>A_Artifact</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>A_Project</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Here is POM.xml of project B (depends on A ) :
<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>B_Group</groupId>
<artifactId>B_Artifact</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>B_Project</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>A_Group</groupId>
<artifactId>A_Artifact</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Here is code snippet from project B:
import packageFromA.*;
public class App {
public static void main(String[] args) {
classFromA ca = new ClassFromA; //from packageFromA
ca.someFunction();
}
}
I run mvn install for A , then for B with no errors But when trying to run code above from IntelliJ Idea, got error : Error:(3, 18) java: package packageFromA does not exist
As I understand from maven docs : "compile dependencies are available in all classpaths, and they are packaged". But it seems that imported class still was not resolved . What was missing in the definitions ? Thanks in advance
UPDATE: When running from IntelliJ the issue was resolved after re-import. But when I run from command linejava projectB
Do I need explicitly set classpath of imported jars? If yes do I need point to i's location im local maven repository (.m2/repository) ? Thanks
In your POM for project B you can add a configuration for the maven-jar-plugin to generate a MANIFEST file which contains the classpath:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
...
</plugins>
</build>
</project>
This will set up the classpath attribute in your MANIFEST file, which expects all dependend jar libraries in a sub directory lib/
of your main jar. You need to copy these libraries manually in the lib
directory.
To distribute your application with all dependend libraries using maven, you can use the maven assembly plugin.
Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With