Home Java javaTutorial Java Maven Build Tool: Getting Started Guide

Java Maven Build Tool: Getting Started Guide

Apr 17, 2024 pm 06:39 PM
linux java apache maven java application

Maven is an open source tool for building and managing Java projects. It provides features such as dependency management, build automation, and documentation generation. After installing Maven, you can create a project by running the mvn archetype:create command. The pom.xml in the project directory contains metadata about the project, build instructions, and dependencies. Building Java applications using Maven involves compiling source code, running tests and creating jar files. You can run the application by executing the mvn clean package command and running the jar file with the java -jar command.

Java Maven构建工具:上手指南

Java Maven Build Tool: Getting Started Guide

Maven is a popular open source tool for building and managing Java projects. It provides rich functionality, including dependency management, build automation, and project documentation generation.

Install Maven

First, install Maven in your system. You can download the latest version from the Maven official website: https://maven.apache.org/.

Extract the downloaded file and add it to the system path. For Windows users:

set PATH=%PATH%;C:\path\to\maven\bin

For Mac and Linux users:

export PATH=$PATH:/path/to/maven/bin

Create Project

To create a Maven-based project, run the following command :

mvn archetype:create -DgroupId=my.group.id -DartifactId=my-project -Dversion=1.0-SNAPSHOT

This command will create a project directory containing basic Maven configuration files and structure.

pom.xml

The pom.xml file in the project directory is the main configuration file of Maven, containing metadata about the project, Build instructions and dependencies.

<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>my.group.id</groupId>
  <artifactId>my-project</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

</project>

Practical Case: Building a Java Application

The following is an example of using Maven to build a simple Java application:

// App.java
public class App {
    public static void main(String[] args) {
        System.out.println("Hello Maven!");
    }
}
// pom.xml
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>my.group.id</groupId>
  <artifactId>maven-test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>11</source>
          <target>11</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Execute the build

In order to build the project, run the following command:

mvn clean package

This command will compile the source code, run the tests and create an executable jar file.

Run the application

To run the application, use the following command:

java -jar target/maven-test-1.0-SNAPSHOT.jar

You will see "Hello Maven!" output in the console .

Conclusion

Maven is a powerful build tool that simplifies the development and maintenance of Java projects. By following this guide, you will understand its basic concepts and how to use it to build and run Java applications.

The above is the detailed content of Java Maven Build Tool: Getting Started Guide. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1596
276
What is a deadlock in Java and how can you prevent it? What is a deadlock in Java and how can you prevent it? Aug 23, 2025 pm 12:55 PM

AdeadlockinJavaoccurswhentwoormorethreadsareblockedforever,eachwaitingforaresourceheldbytheother,typicallyduetocircularwaitcausedbyinconsistentlockordering;thiscanbepreventedbybreakingoneofthefournecessaryconditions—mutualexclusion,holdandwait,nopree

How to clean up your Linux system How to clean up your Linux system Aug 22, 2025 am 07:42 AM

Removeunusedpackagesanddependencieswithsudoaptautoremove,cleanpackagecacheusingsudoaptcleanorautoclean,andremoveoldkernelsviasudoaptautoremove--purge.2.Clearsystemlogswithsudojournalctl--vacuum-time=7d,deletearchivedlogsin/var/log,andempty/tmpand/var

How to use Optional in Java? How to use Optional in Java? Aug 22, 2025 am 10:27 AM

UseOptional.empty(),Optional.of(),andOptional.ofNullable()tocreateOptionalinstancesdependingonwhetherthevalueisabsent,non-null,orpossiblynull.2.CheckforvaluessafelyusingisPresent()orpreferablyifPresent()toavoiddirectnullchecks.3.Providedefaultswithor

Java Cryptography Architecture (JCA) for Secure Coding Java Cryptography Architecture (JCA) for Secure Coding Aug 23, 2025 pm 01:20 PM

Understand JCA core components such as MessageDigest, Cipher, KeyGenerator, SecureRandom, Signature, KeyStore, etc., which implement algorithms through the provider mechanism; 2. Use strong algorithms and parameters such as SHA-256/SHA-512, AES (256-bit key, GCM mode), RSA (2048-bit or above) and SecureRandom; 3. Avoid hard-coded keys, use KeyStore to manage keys, and generate keys through securely derived passwords such as PBKDF2; 4. Disable ECB mode, adopt authentication encryption modes such as GCM, use unique random IVs for each encryption, and clear sensitive ones in time

Java Persistence with Spring Data JPA and Hibernate Java Persistence with Spring Data JPA and Hibernate Aug 22, 2025 am 07:52 AM

The core of SpringDataJPA and Hibernate working together is: 1. JPA is the specification and Hibernate is the implementation, SpringDataJPA encapsulation simplifies DAO development; 2. Entity classes map database structures through @Entity, @Id, @Column, etc.; 3. Repository interface inherits JpaRepository to automatically implement CRUD and named query methods; 4. Complex queries use @Query annotation to support JPQL or native SQL; 5. In SpringBoot, integration is completed by adding starter dependencies and configuring data sources and JPA attributes; 6. Transactions are made by @Transactiona

An In-Depth Guide to Systemd for modern Linux Systems An In-Depth Guide to Systemd for modern Linux Systems Aug 23, 2025 pm 12:02 PM

Systemdisthefirstprocess(PID1)inmodernLinuxsystems,replacingolderinitsystemslikeSysVinitandUpstart,responsibleforbooting,managingservices,devices,logs,andusersessionsthroughasuiteofintegratedtools.2.Itusesunitfiles(.service,.timer,.socket,etc.)todefi

How to use the Pattern and Matcher classes in Java? How to use the Pattern and Matcher classes in Java? Aug 22, 2025 am 09:57 AM

The Pattern class is used to compile regular expressions, and the Matcher class is used to perform matching operations on strings. The combination of the two can realize text search, matching and replacement; first create a pattern object through Pattern.compile(), and then call its matcher() method to generate a Matcher instance. Then use matches() to judge the full string matching, find() to find subsequences, replaceAll() or replaceFirst() for replacement. If the regular contains a capture group, the nth group content can be obtained through group(n). In actual applications, you should avoid repeated compilation patterns, pay attention to special character escapes, and use the matching pattern flag as needed, and ultimately achieve efficient

python numpy where multiple conditions example python numpy where multiple conditions example Aug 21, 2025 am 02:32 AM

When using np.where() to process multiple conditions, you need to wrap the conditions in brackets and replace and or with &,|. 1. Example of multi-condition "And" operation: (arr>3)&(arr

See all articles