0

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.

1
  • What do you mean by a "Java program"? In general, you declare dependencies on artifacts, typically Jars. Commented Oct 25, 2014 at 18:38

4 Answers 4

2

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)...

Sign up to request clarification or add additional context in comments.

4 Comments

@AswinJoseRoy You do not need to add any jar file of first project into another project, you just add dependency into another project. See my post.
no. I guess you build old application on the same machine. so it is already in the local maven repository (~/.m2/repository/...). IDEA will automatically resolve this dependency and required jar files.
@ursa, thanks. But, this is the case because I'm running my program in local. What should I do when I want to run the program in a remote system? Like a cluster deployment.
to run or to build? if to run, then you need to read about applications packaging with maven assembly plugin. if to build - you need to deploy you old application into repo (you company internal or sonatype).
1

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 'mvn install' part is what I missed. All I did was package it. Thanks for the help.
0

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

0

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>
    

1 Comment

do you ever see a java application within a single jar? in your approach maven will generate an empty pom.xml without dependencies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.