Common Maven commands
Keywords: Maven Common commands
1. Common commands
MVN compile
Compile the source code of the main program and do not compile the source code of the test directory. During the first running, the related dependent packages will be downloaded, which may be time-consuming.
MVN test-compile
Compile the test code. After compile, the target folder is generated. The main program is compiled under classes, and the test program is placed under test-classes.
MVN Test
Run unit tests in the Application
MVN site
Websites that generate project-related information
MVN clean
Clear the generated results in the target directory
MVN package
Generate a jar file based on the project and compile and test it before packaging.
MVN install
Install jar in local repository.
MVN Eclipse: Eclipse
Generate the Eclipse project file and package reference definition. Note that you must define classpath variables: m2_repo to point to the local Maven class library directory.
2. Pom. XML Description
<? XML version = "1.0" encoding = "UTF-8"?>
<Project>
<Modelversion> 4.0.0 </modelversion>
<Groupid> com. Tutorial. struts2 </groupid>
<Artifactid> tutorial </artifactid>
<Packaging> war </packaging>
<Version> 1.0-Snapshot </version>
<Name> struts 2 starter </Name>
<URL> http://www.myComp.com </URL>
<Description> struts 2 starter </description>
<Dependencies>
<! -- JUnit -->
<Dependency>
<Groupid> JUnit </groupid>
<Artifactid> JUnit </artifactid>
<Version> 4.4 </version>
<Scope> test </scope>
</Dependency>
<Dependency>
<Groupid> org. springframework </groupid>
<Artifactid> spring </artifactid>
<Version> 2.5.5 </version>
</Dependency>
</Dependencies>
</Project>
Note:
Modelversion object model version used by POM. xml
The unique ID of the organization or group in which the groupid creates the project
Artifactid> unique project id, project name
Packaging package extensions (jar, war, and ear)
Version project version number
Name display name, used to generate a document
URL organization site, used to generate documents
Description project description, used to generate documents
Scope of dependency to manage dependency deployment. The values are as follows:
Default Value of compile, which is used for all stages and released along with the project;
Provided expects JDK, container, or user to provide this dependency. Such as servlet. jar;
Runtime is only used during running;
Test is only used for testing and is not released with the project;
System must provide local jar explicitly, which is not found in the code repository;
3. Create a Maven WEB Project
MVN archetype: generate-dgroupid = com. Demo-dartifactid = web-app-darchetypeartifactid = Maven-Archetype-webapp
Groupid organization name, which corresponds to the package of the project; artifactid project name; archetypeartifactid project type
MVN-declipse. workspace = C:/Eclipse/workspace Eclipse: Add-Maven-Repo
Specify a workspace for eclipse. "C:/Eclipse/workspace" is the workspace of Eclipse.
- MVN Eclipse: Eclipse
Create an Eclipse project file for the project. Converting a Maven project into an Eclipse project
- MVN-O war: exploded releases modified JSP and other engineering files offline
MVN-o
Package-Dmaven. Test. Skip =
True
Offline packaging project, excluding test code
- MVN compile hibernate3: hbm2ddl creates a database table through the hibernate ing file.
MVN-O appfuse: Gen-dentity = teacher
MVN-O appfuse: Install-dentity = teacher
The appfuse framework is used to automatically generate program files such as Dao, service, action, test, and JSP corresponding to the object class.
MVN install: Install-file-dgroupid = javax. transaction-dartifactid = JTA-dversion = 1.0.1b-dpackaging = jar-dfile = D:/test/ssh/webroot/WEB-INF/lib/JTA. jar
Install the local jar package in the application. "D:/test/ssh/webroot/WEB-INF/lib/JTA. Jar" is the local package
The running command of maven2 is MVN,
Common commands:
MVN archetype: Create a Maven Project
MVN compile: Compile source code
MVN test-compile: Compile the test code
MVN test: run the unit test in the application.
MVN site: the website that generates project-related information
MVN clean: clears the generated results in the target directory.
MVN package: generate a jar file based on the project
MVN install: Install jar in local repository
MVN Eclipse: generate an Eclipse project file
Generate a project
Create a Java project: MVN archetype: Create-dgroupid = com. Demo-dartifactid = app
Create a WEB Project: MVN archetype: Create-dgroupid = com. Demo-dartifactid = web-app-darchetypeartifactid = Maven-Archetype-webapp
Generate an Eclipse project
Normal Eclipse project execution: MVN Eclipse: Eclipse
Eclipse WTP project execution: MVN Eclipse: eclipse-dwtpversion = 1.0
(Wtp1.0 and later versions can be used)
Introduction to Basic Nodes of POM. xml files
<Project>: Root Node of the file.
<Modelversion>: the object model version used by POM. xml.
<Groupid>: the unique ID of the organization or group that creates the project.
<Artifactid>: the unique ID of a project, which can be considered as the project name.
<Packaging>: the extension of the package, which generally includes jar, war, and ear.
<Version>: version number of the product.
<Name>: Project display name, which is commonly used in Maven-generated documents.
<URL>: the site of the Organization, which is commonly used for documents generated by Maven.
<Description>: project description, which is commonly used in Maven-generated documents.
In pom 4, <dependency> also introduces <scope>, which mainly manages the deployment of dependencies.
Currently, <scope> can use five values:
Compile: the default value. It is applicable to all stages and will be released along with the project.
Provided: similar to compile, it is expected that JDK, container, or user will provide this dependency. For example, Servlet. jar.
Runtime: used only at runtime, such as the JDBC driver. Applicable to the running and testing stages.
Test: used only for testing. It is used to compile and run test code. Will not be released along with the project.
System: similar to provided, You need to explicitly provide jar containing dependencies. MAVEN will not find it in repository.
Define Your Own structure
Create an archetype project:
MVN archetype: Create/
-Dgroupid = cn. Prof/
-Dartifactid = prof-Archetype/
-Darchetypeartifactid = Maven-Archetype
Main template file: archetype-resources/POM. xml
Modify content
Modify related sources defined in META-INF/Maven/archetype. xml
Install this project: MVN install
Run the following command to create a project in the new directory:
MVN archetype: Create-dgroupid = com. mergere. mvnbook/
-Dartifactid = proficio-example/
-Darchetypegroupid = com. XXX. MVN/
-Darchetypeartifactid = prof-Archetype/
-Darchetypeversion = 1.0-Snapshot
Directory description
Maven2 directory
Directory
Level 2 Directory
Level 3 directory
Level 4 directory
Description
Pom. xml
Maven2 project settings file
Src/
Source code directory
Main/
Project subject directory Root
Java/
Source code directory
Resources/
Required Resource Directory
Filters/
Resource filter file directory
Assemby/
Assembly Descriptors
Config/
Configuration File root directory
Webapp/
Web application directory
WEB-INF/
WEB-INF directory
Test/
Project test directory Root
Java/
Test code directory
Resources/
Resource Directory required for testing
Filters/
Test resource filter file directory
Site/
Site-related resource directories
Target/
Output directory Root
Classes/
Project subject output directory
Test_classes/
Project test output directory
Site/
Project Site output directory
Maven2 has a clear concept of life cycle and provides corresponding commands, making the project build clearer and clearer. Main lifecycle phases:
Verify that the project is correct and all required resources are available.
Compile the source code of the project.
Test-compile: compile the project test code.
Test: Use the compiled test code to test the compiled source code.
Package: A published format, such as jar, which packages compiled source code.
Integration-test: process and release packages in an environment where the integration test can run.
Verify, run any check to verify whether the package is valid and meets quality standards.
Install the package in the local repository, which can be used by other projects as dependencies.
Deploy is executed in the integrated or released environment. The final version package is copied to a remote repository, so that other developers or projects can be shared.
Generate-sources to generate any additional source code required by the application, such as XDoclet.
If you want to compile the project, enter MVN Compile directly. For other stages, you can do so. There is a dependency between stages, for example, test depends on test-compile. When executing MVN test, MVN test-compile is run first, and then MVN Test
Maven2 has the concept of lifecycle, So if you execute a package, the corresponding previous steps, such as compile and test, will be automatically executed.
At the beginning, the execution will be slow. You need to download all the files from the maven2 remote Library to the local machine. If your local database does not have a corresponding dependency package, Maven downloads the package remotely each time. Therefore, it is important to configure an image library.
Problems encountered
1. This error is reported when the m2eclipse plug-in is installed.
Eclipse is running in a JRE, but a JDK is required
Some Maven plugins may not work when importing projects or updating source folders.
Solution: Check Eclipse/readme/readme_eclipse.html for details. In the selecting a workspace section
Create a shortcut and set the target location to D:/eclipse_jee/eclipse.exe-vm d:/Java/jdk1.5.0 _ 10/bin/javaw.
Because the default VM is JDK/JRE/bin/javaw, directly change it to JDK to eliminate errors.
2. Run the command MVN Eclipse: After eclipse-dwtpversion = 1.0, the Java of the imported project facet is still 1.4, and the value set on Eclipse is 5.0.
Solution: add the Plugins instructions in POM. xml.
XML Code
<Plugins>
<Plugin>
<Artifactid> Maven-compiler-plugin </artifactid>
<Configuration>
<Sources> 1.5 </source>
<Target> 1.5 </Target>
</Configuration>
</Plugin>
</Plugins>
<Plugins> <plugin> <artifactid> Maven-compiler-plugin </artifactid> <configuration> <source> 1.5 </source> <target> 1.5 </Target> </Configuration> </plugin> </plugins>
3. After using the command MVN Eclipse: Eclipse, import it to eclipse. Right-click the Eclipse project and choose plug-in Maven> enable dependency management. An error indicating repeated references will be reported.
Solution: Execute MVN Eclipse: Eclipse, refresh the Eclipse project, and cancel the reference dependency of the maven plug-in. The error will be eliminated.
4. Failure executing javac, but cocould not parse the error:
An exception occurs in the compiler (1.5.0 _ 10. If this error is not found in bug parade, archive it in Java Developer Connection (http://java.sun.com/webapps/bugreport. Please include your program and the following diagnostic information in the report. Thank you for your cooperation.
Java. NiO. bufferoverflowexception
Solution: (temporary) set the local language to English in the area and language options in the control panel