Getting Started (a) Maven's use

table of Contents

Today is the Dragon Boat Festival Oh, yesterday university students will give up. Head foam cake thief pull incense, ah ha, a little beside the point; after students went home to his chair; I have found with the same book, namely: "maven combat"; remember from scouring second-hand shop to have been I bought a small half a year, also look at the directory; intend to Dragon Boat Festival with a time of day to give it bite down. . At this point, stand a little Flag ...

image.png

目录

maven specifications:

by default:

  1. Item master code is src / main / java directory compiled ,, at target / classes
  2. Test item code is in src / test / java, compiled under the target / test-class in.
  3. Maven build all inputs in the target / directory.
  4. Automatic access to the central Maven repository, http://repol.maven.org/maven2
  5. The default packaging type jar, package names package main code is output to the target / directory The artifact-version.jar rules.
  6. Placed pom.xml in the project root directory

Note: If pom.xml in scope (range dependent) The default value is compile; if you see a designated test, namely: the dependency is only valid for the test code, if used in the main code will cause compile errors.

mvn clean compile

Implementation process behind:

  • First perform clean: clean task, displaying the target / directory.
  • Then execute resources: resources tasks, deal with project resources.
  • The last execution compiler: compile task compiles the code to the project's main target / classes directory.
    NOTE: The above-mentioned clean: clean ... refers Maven plug and the target, eg: compiler: compile target compiler is complie plug.
mvn clean test

Implementation process behind:

Note: Before performing the test, Maven to automatically execute the project's main processing resources, the main code compile, test resource processing, test code is compiled, etc., it is that characteristic Maven lifecycle. That is, it will first execute the command mvn clean compile behind the operation.

  • First execution resources: testResources task
  • Then execute compiler: testCompile task
  • The last execution surefire: test tasks, the implementation of test and output test report.
    NOTE: The above-mentioned clean: clean ... refers Maven plug and the target, eg: compiler: compile target compiler is complie plug.
mvn clean package

Behind the process execution:
execution jar: jar task, responsible jar packaging.

mvn clean install

Role: jar package project output will be installed to the local Maven repository, Maven for other projects.

FAQ module

1. Why pom.xml need to manually configure the compiler plug it?

例如:
<project>
...
<bulid>
    <plugins>
        <plugin>
            <groupId> org.apache.maven.plugins
            </groupId>
            <artifactId>
            maven-compiler-plugin
            </artifactId>
            <configuration>
                <source> 1.8  </source>
                <target> 1.8 </target>
            </configuration>
        </plugin>
    </plugins>
</build>
...
</project>

Solution: Due to historical reasons, one of the core Maven plugin compiler plug-ins by default only supports Java 1.3, you need to configure the plug-in to support the current version of the JDK.

2. How to build an executable jar file?

Note: The default packet generated not directly run the jar, because there is no class information added to the main method of the manifest [META-INF / MANIFEST.MF file is not disposed in the Main-Class jar file]. We need to use maven-shade-plugin plugin.
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.1.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <!-- 打成可执行的jar包 的主方法入口--> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.golden.MainTest</mainClass> </transformer> </configuration> </execution> </executions> </plugin>

Use Archetype build the project skeleton

mvn archetype:generate

Implementation process behind:

  • Maven2 is not recommended to change the order, which may not be safe, Maven2 will automatically download the latest version Archetype plug-in may be unstable. The Maven3 will automatically download the latest and most stable version, it is safe to use. Maven2 recommended to be manually specified command format is: mvn groupId: artifactId: version: goal [plugin goal], eg: mvn org.apache.maven.plugins: maven-archetype-plugin: 2.0-alpha-5: generate

Guess you like

Origin www.cnblogs.com/zhaojinxin/p/10994624.html