I will show using IntelliJ:
- Right click on the project-> Add Framework Support
2. Now select Maven
Now the pom.xml will be opened and give values for groupId, artifactId and version as your choice
Since the project was not maven project, find if it was using and libraries.
Right click on the project rot and click -> Open Module Settings
Under project setting->Libraries you will find all the libraries being used
We will add these in the pom.xml file
Goto pom.xml and right click->Generate (You can also press alt+insert)
Now click on Dependency
In the serach box, search for the jar file, for our above example we will add commons-io version 2.4
You will see that the dependency is automatically added
Similarly add all the other dependencies.
Add external dependencies
Look if there is any external libraries in the project
So, I have Java 1.8 and JUnit 4.
Let’s add those!
Add Java 1.8
Add the below section outside dependencies section:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Now add the JUnit4
Add it like before (generate->Dependency…)
Just add <scope>test</scope>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
How to add a Jar which is not available in Maven central repo?
Best way to add that in your companies repository, you can set up using Nexus, or Artifactory or something similar
But for now, just put that in the lib folder and reference that from maven:
<dependency>
<!--FIXME: add this jar in Nexus and use from there-->
<groupId>give-any-name</groupId>
<artifactId>give-any-name</artifactId>
<version>give-any-version</version>
<scope>system</scope>
<systemPath>${basedir}/lib/name-of-the-jar.jar</systemPath>
</dependency>
Reload
Right click on the pom.xml-> Maven-> Reload project
Compile
Now compile the project to see if there is any error:
You can also go to the project and from command line do:
mvn compile