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.
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)...
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?
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.
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>
Related
I have a java project with this pom.xml in IntelliJ that uses Collections
<?xml version="1.0" encoding="UTF-8"?>
<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>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.collections/google-collections -->
<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
</dependencies>
</project>
I have run this from the command line:
mvn clean install -U
but even this I got this compilation error:
Cannot resolve symbol ImmutableMap
View -> Tool Windows -> Maven projects
You can click on the left most button to reimport all maven projects.
Then run mvn clean install.
Open your project as a Maven Project
If you don't find the maven toolbar, your project has to be open as a maven project in IntelliJ.
In your maven project panel try :
Reimport all Maven projects
Generate Source and Update Folders for all projects
You should perform a mvn clean install before
I am new in maven and I would like to execute 2 java file at the same time. I read a few posts in StackOverflow and because I do have limited knowledge with maven, I couldn't understand how to do it. I attached my pom.xml file here and the command line that I am using in mac terminal is:
mvn compile exec:java -Dexec.mainClass="org.parallel.ParseThread1"
I have another file named: ParseThread2 and every time I am running them separately like this:
mvn compile exec:java -Dexec.mainClass="org.parallel.ParseThread1"
mvn compile exec:java -Dexec.mainClass="org.parallel.ParseThread2"
I want to know if maven is capable of executing both at the same time.
Few posts that i read was:
How to execute multiple ant targets in maven
Executing multiple maven profiles
Maven Build multiple profiles in one go
My pom.xml file is:
<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>org.parallel</groupId>
<artifactId>parallel</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>parallel</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
</project>
See this answer.
mvn compile
java -cp target/classes org.parallel.ParseThread1 &
java -cp target/classes org.parallel.ParseThread2 &
https://github.com/bvanalderweireldt/concurrent-unique-queue
I have tried to set up a Maven dependency within IntelliJ, but I am not sure how the contents of this repository should be built and imported into a Java project. Could someone with more experience please advise on how this is done?
Kind regards,
L
If you want to use this project in another project, you will create a dependency to this using the dependency entry mentioned on the github readme:
<dependency>
<groupId>com.hybhub</groupId>
<artifactId>concurrent-util</artifactId>
<version>0.1</version>
</dependency>
For this, you need the artifact in your local maven repository*. For this, you need to build this project or use a reference from Maven Central (Thanks #Mark Rotteveel )
Clone the project locally, you need to build it in one of the following ways
Build it from the command line: Navigate to the project's location in your shell (bash or cmd) and run mvn install
This will build the project and add the artifact (jar) to the local .m2 repository.
Import to Intellij Idea (File -> New -> From Existing Sources). Once imported, build this project from the "Maven Projects" view.
Once you have done this, you can use this in other projects using the <dependency> entries
*For production ready apps, you may want to have a common maven repository for team your like Nexus or Artifactory and use that to maintain artifacts. You would also have a build system like Jenkins.
In the link you gave it had the dependency Maven entry for that library.
<dependency>
<groupId>com.hybhub</groupId>
<artifactId>concurrent-util</artifactId>
<version>0.1</version>
</dependency>
That entry would need to be nested into you <dependencies> tag. Like the example below.
<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>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.hybhub</groupId>
<artifactId>concurrent-util</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I have a maven project, created using the quickstart archetype.
This is my pom.xml:
<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.dotd</groupId>
<artifactId>marus</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>marus</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>mvnrepository.com</id>
<name>mvnrepository.com</name>
<url>http://www.mvnrepository.com/artifacts</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
This is what I see in Eclipse:
The fact is that in the maven .m2 repository the jar exists and is resolved by eclipse, so I'm able to import and use classes from log4j as you can see in the following screenshot.
Why does eclipse still tell me that the artifact is missing?
UPDATE
As pointed out by Juned Ahsan, it could be a repository issue. So I did browse http://www.mvnrepository.com/artifacts and it shows a 404 error: the repository doesn't exist.
I switched to the official maven repo, http://repo1.maven.org/maven2/, but the error persists.
Log4j is such a popular library and used by so many frameworks. So it must be transitively injected by maven from any other dependent jar and hence it is available for you in your classpath in eclipse.
You can generate maven dependency tree using plugin.
I just installed m2e (the Maven Eclipse plugin) and created a new Maven Project with a Quickstart archetype. I then went to the official Maven repo to pull down GWT 2.5.1's dependencies, and see that it wants you to add the following <dependency> element to your project's pom.xml file:
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt</artifactId>
<version>2.5.1</version>
</dependency>
So, altogether, my pom.xml looks like:
<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>com.me.myorg</groupId>
<artifactId>maven-resolver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>maven-resolver</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt</artifactId>
<version>2.5.1</version>
</dependency>
</dependencies>
</project>
I am getting the following error:
Missing artifact com.google.gwt:gwt:jar:2.5.1
And furthermore, in Eclipse's Package Explorer, under my project's Maven Dependencies library, nothing is resolving.
What's going on here? Thanks in advance!
Update: the contents of ~/.m2/repository/com/google/gwt/gwt/2.5.1 are as follows:
gwt-2.5.1.jar.lastUpdated gwt-2.5.1.pom.sha1
gwt-2.5.1.pom _maven.repositories
This gwt dependencie it's a parent pom for the gwt project. This means that the repo will only contain the hash and the pom, but not the jar.
You need to use two libraries: gwt-servlet and gwt-user:
I've found this configuration for GWT development (maybe you don't need to put the <scope> as described):
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-servlet</artifactId>
<version>2.5.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId>
<version>2.5.1</version>
<scope>provided</scope>
</dependency>
<dependencies>
Remove your dependencies and update your pom with these two shown above.
You can also use the gwt-maven-plugin to manage your gwt project and deploy via maven. I've found this information there.
I hope that works!