maven notes

First, first acquaintance with Maven
Maven is a software engineering construction tool, which can effectively manage multiple projects.
1.1. Install maven 
1.1.1 Installation file directory description

1.1.2 Set the environment variable path
1.1.3 Modify the local warehouse directory
Modify the settings.xml in the conf in the maven directory by

default in C:\Users\Administrator\.m2\repository .

1.2. Create a simple maven project
1.2.1 Create a project
mvn archetype:create -DgroupId=com.zefu.app -DartifactId=TestMaven and press Enter (DartifactId can be understood as the project name, DgroupId can be understood as the package name), 
if you are The first time you run this command, maven will take some time to download the latest toolkit (Maven calls it artifacts) to your local repository. The downloaded data is placed in the C:\Users\Administrator\.m2\repository directory by default.

After the command is executed, you will see that maven has generated a directory named TestMaven, this name is the DartifactId you specified in the command, enter this directory, you will find the following standard project structure: 

src/main/java: source code
src/test/java: test code
src/main/resources: source code resource file
src/test/resources: test code resource file
pom.xml is the Project Object Model or POM of the project. pom.xml:



1.2.2 The mvn command commonly used in maven projects
mvn clean --> means to run the clean operation (the data in the target folder will be cleaned by default)
mvn clean compile --> means to run the clean first and then run the compilation, see code compilation Go to the target folder
mvn clean test--> run clean and test
mvn clean package--> run clean and package
mvn clean install--> run clean and install, the package will be installed into the local warehouse, so that other The project can call
mvn clean deploy--> run clean and publish (published to the private server).
Take the example above:
enter the project path, enter mvn package and press Enter. At this time, the command line will print out various actions, and use the following A piece of information ends: 

After executing the command, you can see that there is something more in the project

java -cp target/TestMaven-1.0-SNAPSHOT.jar com.zefu.app.App
will print the most classic: Hello World


1.3 myeclipse Integrate maven
1.3.1 myeclipse Configure maven environment

1.3.2 Create maven project


2. Maven dependency
2.1 Query dependency packages

All dependencies are stored by coordinates (GAV-->groupId, artifactId, version)
There are some online repositories that provide coordinate queries (http://search.maven.org/#browse
, http://mvnrepository.com)
How does maven search for dependencies? First, it will be queried in the local warehouse. If the local warehouse is not available, it will be queried in the central warehouse (there will be a private server warehouse later)

2.2 Dependent transitivity
 A-->C B-->A ==> B-->C ( This dependency is passed based on the scope of compile) For
example:
user-vo project depends on hibernate-core,
user-dao project depends on user-vo
, then user-dao will depend on hibernate-core, which is user-dao There is no need to add hibernate-core in pom.xml, he will depend on it himself.

2.3 Dependency conflict
a-->b1.0 c-->b1.1 d-->a and c, at this time in the pom.xml of d, whichever dependency is written first, use the version of the dependency written first.
For example:
user-vo depends on log4j-1.2.16, user-log depends on log4j-1.2.9, user-dao depends on user-vo and user-log, then user-dao depends on log4j-1.2.16 or log4j-1.2. What about 9? --Whichever dependency is written first, use the version of the write-first dependency.

a-->b1.0 c-->b1.1 d-->a and c-->b1.0 f-->d,c, if the length of the path is inconsistent, choose the minimum path
f-->b1. 1

If you want to control the dependency package precisely, you can use the dependency exclusion function to control


2.
test: The scope means that the test scope is valid, and this dependency will not be used during compilation and packaging
compile: The scope means the compilation scope is valid, and the dependencies will be stored in the compilation and packaging.
Provided: Dependency: Compiled and tested The process is valid, and it will not be added when the war package is finally generated, such as: servlet-api, because servlet-api, tomcat and other web servers already exist, if repackaged, it will conflict
system: refer to the local jar package
runtime: depend on when running , do not depend on import when compiling : The default dependency scope
for importing dependencies from other poms is compile 2.5 Version management total version number. Branch version number. Minor version number - The change of the total version number of the milestone version generally indicates the change of the total version number of the framework. Change branch version number: generally means that some functions have been added Minor version number: bug fixes on branch versions Milestones: SNAPSHOT-->alpha-->beta-->release-->GA user0.0.1-SNAPSHOT--> user0.0.1-Release--->user1.0.0SHAPSHOT -->user1.0.0-Rlease                            -->user0.1.0-SNAPSHOT-->user0.1.0-Rlease Third, the maven warehouse will first query in the local warehouse, if the local If there is no warehouse, go to the private server warehouse to query, if the private warehouse server does not have it, go to the central warehouse to query. Download the query results to the private server repository and local repository. So at least make sure that the private server has a network.

















3.1 Private server warehouse
3.1.1 Install private server warehouse
nexus (both linux and windows, you can install it yourself if you are interested)
http://192.168.11.253:8081/nexus/index.html#welcome

Default login name: admin default Password: admin123
3.1.2 Private server warehouse introduction
1. The host warehouse. The release repository of the internal project


2.proxy repository.

Warehouse 3.group group warehouse that looks for data from the remote central warehouse . The group repository is used to facilitate developers to set up a repository, which can be set as a collection of other repositories.

3.1.3 Private repository settings Setting
mirroring: Configure in settings.xml
in the conf directory of the maven installation file Permissions nexus private server has three default users admin has all permissions, deployment has upload permissions, anonymous has viewing permissions. 3.1.5 Create a project factory and set permissions 1. Create two factories 2. Configure permissions for the factory 3. Create roles and assign permissions 4. Create users and assign roles 5. Create published factories set in settings.xml in pom.xml




























3.1.6 Upload the jar package to the third-party dependency warehouse

4. Life cycle and plug-ins
4.1 Life cycle
mvn clean install
validate: Verify whether the project is correct and whether all the necessary information for a complete build is available
generate-sources: Generate all the information that needs to be included in the compilation Source code in the process process-sources: process the source code ,
such as filtering some values The source code of the project process-classes: Post-processing the files generated by the compilation, such as bytecode enhancement for Java classes generate-test-sources: Generate all the test source code included in the test compilation process process-test-sources : Process the test source code, such as filtering some values Run the tests using a suitable unit testing framework. These tests should not require the code to be packaged or released as a prepare-package: perform some necessary operations to prepare the package before the actual packaging. This usually produces an expanded processed version of the package (will be implemented in Maven 2.1+)











package: Package the compiled code into a distributable format such as JAR, WAR, or EAR
pre-integration-test: Perform some actions required before integration tests are run. Such as setting up the environment required for integration tests
integration-test: If necessary, process the package and publish it to an environment where the integration tests can run
post-integration-test: Perform some actions required after the integration tests are run. Such as cleaning the integration test environment.
verify: Perform all checks to verify that the package is valid and conforms to the quality specification
install: Install the package to the local repository for other local projects to use as dependencies.

The lifecycle is dependent on plugins.
4.2 maven plugin
5. Build linux private maven server warehouse (nexus)
1. Copy the installation package to the usr/java directory
2. Unzip nexus tar -zvxf nexus-2.5.1-01-bundle.tar.gz
3. Need to be in /usr In the nexus file under /java/nexus-2.5.1-01/bin/jsw/linux-x86-32 (current system version, pay attention to the number of operating system bits), add RUN_AS_USER=username (username such as root user) 
4. Start nexus cd to the /usr/java/nexus-2.5.1-01/bin/jsw/linux-x86-32 directory, and then start nexus with the ./nexus start command. ./nexus status Check nexus startup status, ./nexus stop stop nexus. 

5 Visit nexus http://192.168.230.130:8081/nexus and the interface appears as shown in the figure:
If you can't access the linux firewall, you need to close the linux firewall
1) Permanently effective, will not be restored after restarting
: chkconfig iptables on
Close: chkconfig iptables off
2) Immediately effective, restore after restarting
: service iptables start
Close: service iptables stop


6. Appendix
1. http://wentao365.iteye.com/blog/903396
2. http://bl-east.iteye.com/blog/1246482
3. Find the jar package: http://search.maven.org/# browse
http://mvnrepository.com/artifact/com.belerweb/pinyin4j/2.5.0
4. How to find jar packages: http://blog.csdn.net/xuexin25java/article/details/7967086
5. Build maven in linux environment nexus server:
http://wenku.baidu.com/link?url=HVfdref5vGBzWDwexmY-u6KC_N5fC8YKu7IIUOCyjWb75izkhA6McM08SUsqETlzYKd7TXn1TXEPjUM8_s4rI53H08RCzhC9Q7cVpVrqZPC
http://blog.csdn.net/liu_xing029/article
6. Configure the Maven project to be automatically deployed to tomcat:
http://blog.csdn.net/hecongqi/article/details/8013598
Maven build... Goals: tomcat:deploy
http://www.blogjava.net/zyl/ archive/2006/12/30/91055.html
http://maven.apache.org/repository/index.html

 

Due to the reasons that some pictures cannot be seen, there is a doc document in the download attachment, which can be seen.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326575611&siteId=291194637