Java language learning 2-Maven version Hellow World

Source: Internet
Author: User
Tags log4j

When I first started learning Java, it was always very strange. Why are some open source software always so abnormal? Is it difficult to use Ant or Maven to compile programs? Later, it was discovered that the Eclipse project may fail to import because the Eclipse configuration on each machine may be different, using Ant or Maven can avoid this situation to a large extent, especially Maven. This tool allows you to build the same development environment as engineering developers, the automatic processing of Jar package dependencies frees us from the simple effort of searching for and downloading dependent Jar packages. Therefore, mastering Maven is very helpful for in-depth understanding of the Java language.

In this article, I will build a Maven-based development environment, use Log4j to output Hello World, and explore how to find a Maven project in Eclipse.

First download the latest version Maven: http://maven.apache.org/download.html, the latest version is 3.0.4. Download the binary version and decompress it to a directory.

Create the mproj directory and use Maven to create a project:

set JAVA_HOME=d:\jdk7u6maven_dir\bin\mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.bjcic.learn -DartifactId=myapp

During the first running, it takes some time to download something from the network to build the environment, and some problems will be prompted. Press enter directly for all problems. After that, the myapp directory will be created under the current directory. The most important pom. xml file is generated in this directory, which is the most important configuration file of the mavon project.

Because we need to use third-party libraries such as log4j, we need to add dependencies in mproj \ myapp \ pom. xml:

<dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>      <dependency>        <groupId>org.apache.logging.log4j</groupId>        <artifactId>log4j-api</artifactId>        <version>2.0-beta3</version>        <scope>compile</scope>      </dependency>      <dependency>        <groupId>org.apache.logging.log4j</groupId>        <artifactId>log4j-core</artifactId>        <version>2.0-beta3</version>        <scope>compile</scope>      </dependency>  </dependencies>

Junit is generated by the system and log4j is added by ourselves.

Go to the mproj \ src \ main directory, create the resources directory, and compile the configuration file log4j2. xml required by log4j:

<?xml version="1.0" encoding="UTF-8"?><configuration>  <appenders>    <Console name="STDOUT" target="SYSTEM_OUT">      <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>    </Console>  </appenders>  <loggers>    <logger name="org.apache.log4j.xml" level="info"/>    <root level="debug">      <appender-ref ref="STDOUT"/>    </root>  </loggers></configuration>

We established a test class com. bjcic. learn. AppUtil. java code as follows:

package com.bjcic.learn;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;public class AppUtil {    private static Logger logger = LogManager.getLogger(AppUtil.class.getName());    public void runUtil() {        logger.info("Hello World");    }}

Add the following code to the src/main/java/com/bjcic/learn/App. java generated by the system:

public static void main( String[] args )    {        AppUtil appUtil = new AppUtil();        appUtil.runUtil();    }

Now everything is configured normally. You can start compiling and running.

First, compile the project:

%maven_home%\bin\mvn compile

The system downloads some dependencies and prompts that the compilation is successful. Next we need to install the project (to facilitate Maven running ):

%maven_home%\bin\mvn install

Finally, run:

%maven_home%\bin\mvn exec:java -Dexec.mainClass=com.bjcic.learn.App -Dexec.args="p1 p2"

So far, using Maven 3 to compile and run a project is all done, but it is still working in the command line. No syntax prompt is a painful task, therefore, Maven provides the function of converting a project into an Eclipse project.

%maven_home%\bin\mvn eclipse:eclipse

In this way, you can directly open it in eclipse.

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.