The first step in Maven’s actual combat is to create the hello-world project by eclipse

Time:2019-8-13
  • First create a maven project:

The first step in Maven's actual combat is to create the hello-world project by eclipse

  • Select quikcstart:

The first step in Maven's actual combat is to create the hello-world project by eclipse

  • Fill in the relevant configuration

The three elements gruop ID and artifact ID and version define the basic coordinates of a project. In Maven’s world, any jar, POM or war is distinguished based on these basic coordinates.

Group Id: Define project groups
Artifact Id: Defines the unique ID in the current project group
Version: As the name implies, specify the current version

The first step in Maven's actual combat is to create the hello-world project by eclipse

  • Generated Maven project and pom.xml

The first step in Maven's actual combat is to create the hello-world project by eclipse

  • Next, change the app. java code

![clipboard.png](/img/bV9ApR)

package com.will.hello_world;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( new App().sayHello() );
    }
    
    public String sayHello(){
        return "Hello Maven!";
    }
}

At this point, the target directory is still empty.

The first step in Maven's actual combat is to create the hello-world project by eclipse

When we right-click on the project and click debug as – > maven clean, the console will output the following content, which tells us that the clean action, Maven will first clean the output directory target, that is, delete the directory.

The first step in Maven's actual combat is to create the hello-world project by eclipse

At this point, right-click Pom. XML to select run as – > Maven build

The first step in Maven's actual combat is to create the hello-world project by eclipse
Goals enters compile, and then clicks run, which is typed out by the console.

The first step in Maven's actual combat is to create the hello-world project by eclipse
It shows that Maven has performed the compilation task and placed the compiled class file in the target directory after the workspace specified working directory.

  • Writing unit tests

After compiling, we will start testing. The Maven project we created has integrated JUnit dependencies. If not, we can add it manually and then execute maven update.

<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>com.will</groupId>
  <artifactId>hello-world</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>hello-world</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>



There is also a scope attribute in the POM code mentioned above. If the scope attribute is test, it means that the dependency is valid only for testing. If a dependency is not declared, then the dependency is valid for both the main code and the test.

With test dependencies, you can start writing test classes in the Src / test / Java directory

The first step in Maven's actual combat is to create the hello-world project by eclipse

After completing the test case, click on maven test, the console output the following content, failure and warning are 0, indicating that our test passed.

  • Packaging and operation

After compiling and testing the project, the next important step is packaging. There is no specified packaging type in the pom, and the default packaging type jar is used.
Click on Maven build – > goals to fill in the package, click run, eclipse will help us pack.

The first step in Maven's actual combat is to create the hello-world project by eclipse

Similarly, before packaging, Maven will automatically help us perform compilation, testing and other operations. As you can see, at this time, we have packaged the project into a Jar package of hello-world-0.0.1-SNAPSHOT.jar. If necessary, we can copy the Jar file to use the hel in the classpath file of other projects. The loworld class.

Or if you want to use this jar file in other Maven projects in your locality, you need to do one more step, maven install, which will install the jar package output into your local maven repository, and other Maven projects can use it.

  • Running jar files

At this point, we execute the Java – jar jar package name in the folder where the jar package resides
The following error was reported

The first step in Maven's actual combat is to create the hello-world project by eclipse

Because our generated jar package contains the main method, class information with the main method will not be added to the mainfest (you can use the decompression tool to view the Jar package)

At this point, we can use the maven-shade-plugin plug-in to add the following code to pom.xml

<build>  
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-shade-plugin</artifactId>  
            <version>2.4.1</version>  
            <executions>  
                <execution>  
                    <phase>package</phase>  
                    <goals>  
                        <goal>shade</goal>  
                    </goals>  
                    <configuration>  
                        <transformers>  
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
                                <mainClass>com.will.hello_world.App</mainClass>  
                            </transformer>  
                        </transformers>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>    
    </plugins>  
</build>


Re-execute the Maven clean and Maven build select package command and add a new jar package with shade suffix under the target directory.
The first step in Maven's actual combat is to create the hello-world project by eclipse

Execute java-jar x.jar at this time

The first step in Maven's actual combat is to create the hello-world project by eclipse

Successful implementation!

Recommended Today

Manjaro uses SS method (requires nodejs environment)

Just installed manjaro, need to installshadowsocks-qt5+Proxy SwitchyOmega, but the latest Chrome has long blocked the installation of non Google store CRX channels. Here is my solution, which is troublesome but usable. If you are familiar with NPM commands in nodejs, you won’t find it troublesome, because nodejs is used for compilation. Preparation environment: A VPS […]