Maven series (1) Building Java applications from scratch

In an interview some time ago, the interviewer asked me to briefly describe the Maven life cycle, what? Isn't Maven just used to build a project, what is the life cycle (I have been doing .net before, but I want to find a job on java, I used Maven to build projects before, so I mentioned it)? After I came back, I quickly googled it. After reading it, I was ashamed. I didn't even understand the Maven life cycle. I dared to say that I had used Maven, so I studied it in my spare time. This series is also a record of my learning Maven, which will deepen with my learning. I refer to the spring.io Maven tutorial in my learning .

What is Maven? What is the role of Maven? This, let's answer it in the next article!

1. Create a 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 {
    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!";
    }
}

2. Download, install, configure Maven

Maven official website directly download the compressed file, download address Maven , after decompression, add the bin directory to the environment variable. Run the mvn command in the command line to test whether maven is installed correctly. If the installation is correct, it will display the Maven version number.

mvn -v

3. After installing Maven, we need to create a Maven project definition. Maven projects are defined using an XML file called pom.xml. Among other things, the file provides the project's name, version and its dependencies on external libraries. Create a new pom.xml file in the project folder and copy the following content into the file.

<?xml version="1.0" encoding="UTF-8"?>
<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/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>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>hello.HelloWorld</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

 Apart from the optional <packaging> element, this is the simplest pom.xml file needed to build a Java project. It contains the following details of the project configuration:

<modelVersion>: POM model version (always 4.0.0).

<groupId>: The group or organization this item belongs to, usually expressed as a reverse domain name.

<artifactId>: The name to give the project library artifact (eg 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 WAR file packaging uses "war".

4. Compile the java source code. Run Maven, execute the compile goal. Once done, the compiled .class files can be found in the target/classes directory.

mvn compile

Execute the package target. Maven will recompile the source code, execute some test cases, and finally package the source code into a jar file under /target. The jar file is named based on <artifactId> and <version> in pom.xml. Therefore, according to the configuration of our pom.xml above, the generated jar file should be named gs-maven-0.1.0.jar

mvn package

Use 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

In the above steps, we only established the overall framework of a java project, and cannot directly open the project in eclipse. You need to execute the following commands to generate related projects that can be imported into the IDE (after executing the following commands, it will automatically download and update the relevant resources and configuration information, and generate all the project files required by the Eclipse IDE)

mvn eclipse:eclipse

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324453302&siteId=291194637
Recommended