HOW TO RUN AN EXISTING JAVA PROJECT USING MAVEN WITHOUT ANY IDE

October 11, 2018

wsit_developer

java, maven

Java

0

Hello everyone. Today I am discussing about maven. So the first question is What is Maven?

Maven is a software project management and comprehension tool primarily used with Java-based projects but that can also be used to manage projects written in C#, Ruby, and other languages. Maven helps manage builds, dependencies, software configuration management (SCM).

In short-

  • A standard way to build the project.
  • A clear definition of what the project consisted of
  • An easy way to publish project information and a way to share JARs across several projects.

What is POM?

POM is an XML file that contains information about the project and configuration details used by Maven to build the project. POM stands for Project Object Model.

Many integrated development environments (IDEs) provide plug-ins or add-ons for Maven, thus enabling Maven to compile projects from within the IDE. But now I don’t use any IDE to run this Maven project.But I will use command prompt.

1st step: Setup Maven

  • Make sure JDK is installed and “JAVA_HOME” variable is added as windows environment variable.

  • Add both M2_HOME and MAVEN_HOME variables in the Windows environment, and point it to your Maven folder.
  • Update PATH variable, append Maven bin folder – %M2_HOME%\bin, so that you can run the Maven’s command everywhere.
  • Done, to verify it, run mvn –version in the command prompt.

If you see a similar message, means the Apache Maven is installed successfully on Windows.

2nd step: Create Maven project

  • Select a directory where we want to create the maven project.
  • Go to this directory and open the command prompt.
  • Write: mvn archetype:generate
  • Then it will create a maven project with a proper directory structure. In this process need some ID. You write as you want.
  1. groupId : com.calculator
  2. artifactId : calculator-project
  3. version : 1-0-SNAPSHOT
  4. package : com.calculator
  • After that the project will ready with proper directory structure.

3nd step: POM.xml

Add this in the POM.xml file.

<build>

        <sourceDirectory>src</sourceDirectory>

        <plugins>

            <plugin>

                <artifactId>maven-compiler-plugin</artifactId>

                <version>3.3</version>

                <configuration>

                    <source>1.8</source>

                    <target>1.8</target>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.codehaus.mojo</groupId>

                <artifactId>exec-maven-plugin</artifactId>

                <version>1.2.1</version>

                <configuration>

                    <mainClass>        com.calculator.App</mainClass>

                </configuration>

            </plugin>

        </plugins>

    </build>

4th step: Add your existing .java files

5th step: Build and RUN

Now again open the command prompt in the project root.

To build the project Write:

mvn package

After a successful build, you can see that a .jar has created in target folder named

“Calculator-project-1.0-SNAPSHOT.jar”.

This name will depend on the package name and version.

Now finally RUN the project.

To run the project write:

java -cp target/jarfileName path_of_the_project_startup

java -cp target/Calculator-project-1.0-SNAPSHOT.jar com.calculator.App

And enjoy the Maven.

Post by wsit_developer

Leave a Reply

Your email address will not be published. Required fields are marked *