Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a Maven pom.xml to an existing project without using an IDE?

I have an existing Java project with a single dependency. I just want to make a pom.xml so that someone can pull the project, mvn install or mvn init or whatever it is, then start working. How do I do this without using an IDE?

I'm looking for the equivalent of pip freeze or npm init, or just what I can copy and paste into a new pom.xml file.

I tried Googling and only found stuff for starting new projects with Maven or using an Eclipse plugin.

like image 360
Max Heiber Avatar asked Mar 09 '17 02:03

Max Heiber


2 Answers

Follow suggested three steps to convert your project into maven project.

Step 1: As suggested by nullpointer, following attributes are mandatory in your pom to make it as maven project.

Sample pom.xml 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.infy</groupId>
    <artifactId>user-identity-rest</artifactId>
    <version>1.0.0</version>
</project>

groupId: It is unique identifier to the group of multiple modules in your project

artifactId: Unique module identifier of your module.

version: Initially we will start with version 1.0.0 and will upgrade to 1.0.1 and so on subsequently.

You can customized these things as per your understanding and future references.

Step 2: Now you need to add all libraries in declared pom.xml which is similar to jars you added in your project.

You might have added a library in your project(lets say json-simple-1.1.1.jar). Here maven doesn't look for the whole library, instead it asks its dependency.

You can find dependency of your external library here by searching for library with exact version.

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

Now you need to copy the dependency from given site and paste it to your pom.xml file within the <dependencies> element. In this example, the full pom.xml 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.infy</groupId>
    <artifactId>user-identity-rest</artifactId>
    <version>1.0.0</version>
    <dependencies>
        <dependency>
             <groupId>com.googlecode.json-simple</groupId>
             <artifactId>json-simple</artifactId>
             <version>1.1.1</version>
         </dependency>
    </dependencies>
</project>

Maven will automatically fetch it from its repository.

Step 3: Third and last step is to build your project. Open command prompt/terminal and go to the directory where your pom.xml is declared and type mvn clean install

Building application with maven

This will generate an executable for your project on local and voilla! You have converted and built your project into maven project

Reference Path: C:\PA\IntelliJ Workspace\Springboot Practise\target\spring-boot-examples-1.0-SNAPSHOT.jar

Note: Comment below if you need more clarifications.

like image 189
Pratik Ambani Avatar answered Oct 30 '22 06:10

Pratik Ambani


You seem to be looking for a Minimal POM for your project. This is defined as -

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
</project>

where you can replace the names more convenient to your group and artifact. Just a small note to take care of -

..if the configuration details are not specified, Maven will use their defaults.

like image 44
Naman Avatar answered Oct 30 '22 04:10

Naman