Header Ads Widget

Responsive Advertisement

How to create maven project



Creating a Maven project from the command prompt is straightforward with the help of the Maven command-line interface. Here's a step-by-step guide to creating a Maven project:

1. Install Maven

Before creating a Maven project, make sure Maven is installed on your system. You can download it from Maven's official website and follow the installation instructions.

To verify the installation, run:

sh

mvn -version

 

This command should display the installed Maven version if it's correctly installed.

2. Create a Maven Project

You can use the mvn command to generate a new Maven project. Follow these steps:

  1. Open Command Prompt: Open a terminal or command prompt.
  2. Navigate to the Directory: Change to the directory where you want to create the Maven project.
  3. Run the Maven Archetype Command: Use the following command to create a new Maven project using the default archetype (template):

Sh

mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

 

    • -DgroupId: The group ID for your project (usually the package name).
    • -DartifactId: The name of the project.
    • -DarchetypeArtifactId: The archetype to use. maven-archetype-quickstart is a basic template for Java projects.
    • -DinteractiveMode=false: Runs the command in non-interactive mode, avoiding prompts.
  1. Navigate to the Project Directory: Change to the directory created by the above command:

sh

cd my-app

 

3. Build and Run the Maven Project

  1. Compile the Project: To compile the project, run:

Sh

mvn compile

 

  1. Package the Project: To create a JAR file of your project, run:

sh

mvn package

 

The JAR file will be located in the target directory.

  1. Run the Project (if it's a Java application): Assuming you have a main class with a public static void main(String[] args) method, you can run it using:

sh

mvn exec:java -Dexec.mainClass="com.example.App"

 

You'll need to add the exec-maven-plugin to your pom.xml for the exec:java command to work.

Example Directory Structure

After running the above commands, your project directory should look like this:

structure

my-app/

── pom.xml

└── src/

    └── main/

        └── java/

            └── com/

                └── example/

                    └── App.java

    └── test/

        └── java/

            └── com/

                └── example/

                    └── AppTest.java

 

ü  pom.xml: The Maven Project Object Model file where project dependencies and configurations are specified.

ü  src/main/java: Contains the main source code.

ü  src/test/java: Contains test source code.

 

 

4. Convert Maven Project to Eclipse Project

After creating your Maven project and navigating to its path, you can use Maven to generate Eclipse project files:

  1. Open Command Prompt: Navigate to the project directory.
  2. Run the Maven Eclipse Plugin:

sh

mvn eclipse:eclipse -Dwtpversion=2.0

 

This command generates the necessary Eclipse project files, including .project and .classpath.

5. Import Maven Project into Eclipse

  1. Open Eclipse.
  2. Import the Maven Project:

Ø  Go to File > Import...

Ø  Choose Maven > Existing Maven Projects and click Next.

Ø  Click Browse and navigate to the directory of your Maven project.

Ø  Select the project and click Finish.

Eclipse will import the Maven project and configure it automatically.

6. Include Third-Party JAR Files in Maven

To include third-party JAR files in your Maven project, you need to add dependencies to your pom.xml file. Here’s how you can do it:

  1. Find the Dependency:

Ø  Search for the third-party JAR file you want to include on Maven Central Repository or another Maven repository.

  1. Add Dependency to pom.xml: Once you find the dependency information (groupId, artifactId, version), add it to the <dependencies> section of your pom.xml file. For example:

Xml

<dependencies>

    <dependency>

        <groupId>org.apache.commons</groupId>

        <artifactId>commons-lang3</artifactId>

        <version>3.12.0</version>

    </dependency>

</dependencies>

 

  1. Update Project: After modifying pom.xml, update the Maven project in Eclipse:

Ø  Right-click the project in Eclipse.

Ø  Choose Maven > Update Project...

Ø  Select the project and click OK.

7. Adding JARs Directly (if not available in a repository)

If the JAR file is not available in a public Maven repository, you can add it manually:

  1. Install the JAR into Local Maven Repository:

sh

mvn install:install-file -Dfile=/path/to/your.jar -DgroupId=com.example -DartifactId=my-jar -Dversion=1.0 -Dpackaging=jar

 

Replace /path/to/your.jar with the path to your JAR file and adjust the groupId, artifactId, and version as needed.

Example

mvn install:install-file -Dfile=D:/bitcoin/bitcoinj-master/core/target/bitcoinj-core-0.15-SNAPSHOT.jar -DgroupId=org.bitcoinj -DartifactId=bitcoinj-core -Dversion=0.15-SNAPSHOT -Dpackaging=jar

 

 

  1. Add the Installed JAR as a Dependency: Add the dependency to your pom.xml as shown earlier.

You now have a basic Maven project set up. You can start adding your own code, dependencies, and configurations as needed!






maven project using command prompt
maven project using command prompt






Post a Comment

0 Comments