There are many related questions to this but I am getting confused with the answers from them and decided to ask this myself. I have a Java program and want to use it in another one. How can I add the first one as a dependency in the POM.xml file of the second program? The IDE I am using is IntelliJ version 13.
4 Answers
If the first java program was built with maven (it has pom.xml with groupId:artifactId:version), you can add it as a dependency into your new project:
<dependency>
<groupId>old-program-group-id</groupId>
<artifactId>old-program-artifact-id</artifactId>
<version>version-you-want-to-re-use</version>
</dependency>
If no - it would be very complicated way (in size of an article)...
4 Comments
Suppose this is 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.a</groupId>
<artifactId>a</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>a</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
After you compile project A through mvn install command, you can add project A into project B by using <dependency> of project A
suppose this is pom.xml of project B
<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.maventest</groupId>
<artifactId>mytest2</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>b</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.a</groupId>
<artifactId>a</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
See more information : How do I add a project as a dependency of another project?
1 Comment
The easiest way to make one a dependency of the other would be to put them together under a parent POM. Building multi-module Maven project is easy. This will get your started. Once you have created the parent pom and referenced it in the two modules, just use a standard maven <dependency/> tag in the one needing the dependency and create a new Intellij project that imports from the parent POM.xml file.
Comments
Follow this steps
- Create jar file (java archive) with your project.
Execute
mvn install:install-file -Dfile=c:\your file.jar -DgroupId=your.id -DartifactId=your name a -Dversion=1.0 -Dpackaging=jar
to include at your local repository
Add dependency to pom.xml
<dependency> <groupId>your.id</groupId> <artifactId>your name</artifactId> <version>1.0</version>