Compiling a Java project using MAVEN

Source: Internet
Author: User
Tags local time

Summary: Overview This article demonstrates the time required to compile a Java project with Maven: 15-minute text editor or IDE JDK 6 or later to create a project This example is primarily designed to showcase Maven, so Java projects are simple. Create a project structure select a project directory and use the following statement on the *nix system mkdir-p src/main/java/he

Review

This article demonstrates compiling a Java project with Maven

Need
    • Duration: 15 minutes
    • Text editor or IDE
    • JDK version 6 or later
Create a project

This example is primarily designed to showcase Maven, so Java projects are simple.

Create a project structure

Select a project directory and use the following statement on the *nix system

mkdir -p src/main/java/hello

Use commands under Window

mkdir src\main\java\hello

Create the following structure:

└── src    └── main        └── java            └── hello

src/main/java/helloCreate a Java file under the HelloWorld.java directory andGreeter.java

src/main/java/hello/HelloWorld.java

package hello;public class HelloWorld { public static void main(String[] args) { Greeter greeter = new Greeter(); System.out.println(greeter.sayHello()); }}

src/main/java/hello/Greeter.java

package hello;public class Greeter { public String sayHello() { return "Hello world!"; }}

Now that the project is complete, you can compile it with maven. For MAVEN installation, you can refer to the Apache Maven 3.1.0 installation, deployment, use

Define a simple Maven compilation

First, create a MAVEN project definition file at the root of the project pom.xml , which is primarily a description of the project's name, version, and dependent library

pom.xml

<?xml version= "1.0" encoding= "UTF-8"?><Projectxmlns="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/maven-v4_0_0.xsd" ><modelversion>4.0.0</Modelversion><Groupid>org.springframework</Groupid><Artifactid>gs-maven</Artifactid><Packaging>jar</Packaging><version>0.1.0</Version><Build><Plugins><Plugin><Groupid>org.apache.maven.plugins</Groupid><Artifactid>maven-shade-plugin</Artifactid><version>2.1</Version><Executions><Execution><Phase>package</Phase><Goals><Goal>shade</Goal></Goals><Configuration><Transformers><Transformerimplementation= "Org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" > <mainclass>hello. Helloworld</mainclass> </transformer> </transformers> </configuration> </ execution> </executions> </plugin> </ plugins> </build></PROJECT>             

In addition to the <packaging> elements, other elements are the pom.xml most basic elements of a file. It includes the following configurations for several projects:

    • <modelVersion>: POM module version (usually 4.0.0).
    • <groupId>: The organization number to which the project belongs, usually with a domain name
    • The name of the <artifactId> project (for example, the name of a jar or war)
    • Version number of the <version> project compilation
    • <packaging> Project Package form, jar or war
Compiling Java code

Run the following statement to compile

mvn compile

The compiled .class files will appear in the target/classes directory.

To run the project:

exec:java -Dexec.mainClass="hello.HelloWorld"

The output is as follows:

If you don't want to run the .class file directly, you can package it:

package

After packaging is complete, target a jar file is generated in the directory, with the file name consisting of <artifactId> and <version>. For example, this will be based on the pom.xml generatedgs-maven-0.1.0.jar

If you want to install your project's jar file to the local Maven repository, then you should call the following statement:

install

At this point, your project code will be compiled, tested, packaged, and copied to a local dependent library for other project references.

The above example source address Https://github.com/waylau/maven-demo in the Demo1

When it comes to project dependencies, let's say声明依赖

Claim dependent

The above example is relatively simple and does not use other libraries. But real projects may reference (rely on) to many other libraries.

The following is an example Joda Time of a dependent library.

Modifysrc/main/java/hello/HelloWorld.java

package hello;import org.joda.time.LocalTime;public class HelloWorld { public static void main(String[] args) { LocalTime currentTime = new LocalTime(); System.out.println("The current local time is: " + currentTime); Greeter greeter = new Greeter(); System.out.println(greeter.sayHello()); }}

Running now mvn compile will be an error, because there is no claim dependency. Under the <project> node, insert the following:

<dependencies>    <dependency>        <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.2</version> </dependency></dependencies>

This section declares the dependencies of the project. Each dependent node <dependency> consists of three child nodes:

    • <groupId>: The name of the organization to which the dependent library belongs
    • <artifactId>: Dependent library name
    • <version>: Dependent library version

<scope> is also introduced in,<dependency> in Pom 4, which primarily manages dependent deployments. Currently <scope> can use 5 values:

    • Compile, the default value, applies to all stages and is published along with the project.
    • provided, similar to compile, expects the JDK, container or user to provide this dependency. such as Servlet.jar.
    • runtime, used only at runtime, such as the JDBC driver, for the run and test phases.
    • Test, used only during testing, to compile and run the test code. Will not be published with the project.
    • System, similar to provided, needs to explicitly provide an jar,maven that contains dependencies and does not look for it in repository.

Now that you run mvn compile or mvn package , maven will automatically download dependencies.

Full-pom.xml

<?xml version= "1.0" encoding= "UTF-8"?><Projectxmlns="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/maven-v4_0_0.xsd" ><modelversion>4.0.0</Modelversion><Groupid>org.springframework</Groupid><Artifactid>gs-maven</Artifactid><Packaging>jar</Packaging><version>0.1.0</Version><!--tag::joda[]-<Dependencies><Dependency><Groupid>joda-time</Groupid><Artifactid>joda-time</Artifactid><version>2.2</Version></Dependency></Dependencies><!--end::joda[]-<Build><Plugins><Plugin><Groupid>org.apache.maven.plugins</Groupid><Artifactid>maven-shade-plugin</Artifactid><version>2.1</Version><Executions><Execution><Phase>package</Phase><Goals><Goal>shade</Goal></Goals><Configuration><Transformers><Transformerimplementation= "Org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" > <mainclass>hello. Helloworld</mainclass> </transformer> </transformers> </configuration> </ execution> </executions> </plugin> </ plugins> </build></PROJECT>             

To run the project:

exec:java -Dexec.mainClass="hello.HelloWorld"

The output is as follows:

The above example source address Https://github.com/waylau/maven-demo in the Demo2

Reference: http://spring.io/guides/gs/maven/

http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/

Original: https://yq.aliyun.com/articles/46992

Compiling a Java project using MAVEN

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.