Maven Series (i) Build Java applications from scratch

Source: Internet
Author: User

Before the interview, the interviewer asked me to briefly describe the MAVEN lifecycle, what? Isn't MAVEN building a project, what's The life cycle (I've been doing. NET before, but I'm looking for a job on Java and I've built a project with Maven before, so I mentioned it)? Come back after Baidu a bit, watching the moment of shame, even maven life cycle do not understand, also dare to call the use of Maven, so free time to learn a bit. This series is also my study in Maven in a record, will follow my study in depth and deepen, the study referred to Spring.io maven tutorial.

What is maven? What is Maven's role? Here, let's answer the next article!

1, create the directory structure, as follows

└──SRC    └──main        └──java            └──hello

In the directory Src/main/java/hello, we can add any Java class. So let's create these two classes: Helloworld.java and Greeter.java.

Src/main/java/hello/helloworld.java

 Package Hello;  Public class HelloWorld {    publicstaticvoid  main (string[] args) {        new  Greeter ();        System.out.println (Greeter.sayhello ());}    }

Src/main/java/hello/greeter.java

 Package Hello;  Public class Greeter {    public  String SayHello () {        return "Hello world!" ;    }}

2. Download, install, configure MAVEN

MAVEN website directly download compressed files, MAVEN, unzip, add bin directory to environment variables. Run the MVN command on the command line to test if MAVEN is installed correctly. If installed correctly, the MAVEN version number is displayed.

Mvn-v

3. After installing MAVEN, we need to create a MAVEN project definition. The MAVEN project is defined using an XML file named Pom.xml. In addition to this, the file also provides the name of the project, the version and its dependencies on the external library. Create a new Pom.xml file under the project folder and copy the following to the file.

<?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>    <Properties>        <Maven.compiler.source>1.8</Maven.compiler.source>        <Maven.compiler.target>1.8</Maven.compiler.target>    </Properties>    <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 optional <packaging> elements, this is the simplest pom.xml file that is required to build a Java project. It contains the following details about the project configuration:

<modelversion>: Pom model version (always 4.0.0).

<groupid>: The group or organization to which the project belongs, usually represented as a reverse domain name.

<artifactid>: The name of the project library artifact to be given (for example, the name of its jar or war file).

<version>: The project version being built.

<package>: How the project should be packaged. JAR file packaging defaults to "jar", and the war file is packaged using "war".

4, compile Java source code. Run Maven and execute the compile (compile) target. When you are finished, you can find the compiled. class file in the Target/classes directory.

MVN Compile

Executes the packaging (package) target. MAVEN will recompile (compile) The source code, execute some test use cases, and finally package the source into the jar file under/target, and the jar file is named based on the <artifactId> and < in Pom.xml Version>. Therefore, based on the configuration of the pom.xml above, the generated jar file should be named Gs-maven-0.1.0.jar

MVN Package

Using the Java command to execute the main method in the jar

Java-jar target/gs-maven-0.1. 0. jar

6. Import the Java project into eclipse

The above steps are all just a framework for building a Java project as a whole, and we cannot open the project directly in Eclipse. You need to execute the following command to generate the related project that will be converted to the IDE (after executing the following command, it will automatically download the update related resources and configuration information and produce all the project files required by the Eclipse IDE)

MVN Eclipse:eclipse

Maven Series (i) Build Java applications from scratch

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.