Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Install and deploy Maven tutorials

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

What this article shares with you is the method of installing and deploying Maven. I believe most people have not yet learned how to deploy. In order to let you learn, I have summarized the following contents. Without saying much, let's read on.

The installation of Maven depends on the java environment, so you should deploy the jdk environment!

1) configure jdk environment

[root@maven ~] # rpm-qa | grep jdk / / query native default JDK environment java-1.8.0-openjdk-headless-1.8.0.161-2.b14.el7.x86_64copy-jdk-configs-3.3-2.el7.noarchjava-1.7.0-openjdk-headless-1.7.0.171-2.6.13.2.el7.x86_64java-1.7.0-openjdk-1.7.0 .171-2.6.13.2.el7.x86_64java-1.8.0-openjdk-1.8.0.161-2.b14.el7.x86_64 [root@maven ~] # yum-y remove java-1.7.0-openjdk-headless-1.7.0.171-2.6.13.2.el7.x86_64 [root@maven ~] # yum-y remove java-1.8.0-openjdk-headless-1.8.0.161-2.b14 .el7.x86 _ 64 / uninstall the original jdk-headless package [root@maven] # tar zxf jdk-8u211-linux-x64.tar.gz-C / usr/local / / decompress the provided JDK package [root@maven] # vim / etc/profile / / write system environment variables Add the following at the end: export JAVA_HOME=/usr/local/jdk1.8.0_211 / / specify the Java installation path export JRE_HOME=/usr/local/jdk1.8.0_211/jre / / Java runtime environment export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar / / declare the classes in Java Class is the property of a kind of transaction export PATH=$JAVA_HOME/bin/:$JRE_HOME/bin/:$PATH / / add the defined variable to the system environment variable [root@maven ~] # source / etc/profile / / reload the system environment variable file [root@maven ~] # java-version / / query support Java version information java version "1.8.0U211" / / meets the requirements Java (TM) SE Runtime Environment (build 1.8.0_211-b12) Java HotSpot (TM) 64-Bit Server VM (build 25.211-b12) Mixed mode) 2) deploy Maven [root@maven ~] # wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz# can directly use the apache-maven-3.6.1-bin.tar.gz [root@maven ~] # tar zxf apache-maven-3.6.3-bin.tar.gz-C / usr/src [root@maven] in the network disk. ~] # mv / usr/src/apache-maven-3.6.3/ / usr/local/maven [root@maven ~] # vim / etc/profile # add the following content export MAVEN_HOME=/usr/local/mavenexport PATH=$PATH:$MAVEN_HOME/ [root @ maven ~] # source / etc/profile [root@maven ~] # mvn-v # is deployed successfully (because it is deployed in a binary way) Apache Maven 3.6.3 (cecedd343002696d0abb0b32b541b8a6ba2883f) Maven home: / usr/local/mavenJava version: 1.8.0mm 211 Vendor: Oracle Corporation, runtime: / usr/local/jdk1.8.0_211/jreDefault locale: zh_CN, platform encoding: UTF-8OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix" 1) configuration uses Aliyun warehouse address

Due to the use of the address of the maven official website. Download is slow, so directly change the maven warehouse address provided for Aliyun!

[root@maven ~] # vim / usr/local/maven/conf/settings.xml# navigate to line 158and fill in the content aliyun central aliyun maven https://maven.aliyun.com/nexus/content/groups/public/ # must be written in this tag. # mirror is an image, which mainly provides a convenient way to switch remote warehouse addresses. # central is the id of the warehouse to be replaced. Represent the original warehouse. If you fill in * to replace all warehouses 2) build the project to test [root@maven ~] # mkdir / tmp/testdir [root@maven ~] # cd / tmp/testdir/# execute the following command to test [root@maven testdir] # mvn archetype:generate-DgroupId=com.lzj.maven.quickstart-DartifactIdk=testapp-DarchetypeArtifactId=maven-archetype-quickstart# to create a maven project (Java type) Can add-DinteractiveMode=false cancel interaction process.. # omit part of the content Define value for property 'artifactId': testapp # need to manually specify the project name, can be customized, and then keep the default! [INFO] BUILD SUCCESS # when this prompt appears, it indicates that the construction was successful # the above command is explained as follows: # archetype:generate: create a simple java project; # archetype, that is, a prototype, is a Maven plug-in, to be exact, a project template whose task is to create a project structure based on the template. # create a simple java application using the quickstart prototype plug-in #-DgroupId: organization name, reverse of the company URL (reverse domain name) + project name; #-DartifactIdk: project name or module name; #-DarchetypeArtifactId: specify ArchetypeId,maven-archetype-quickstart to create a simple Java application; #-DinteractiveMode: whether to use interactive mode. 3) Parameter interpretation of the generated POM file

The contents generated by the current directory:

[root@maven testdir] # ls # after the above command is executed successfully, a directory named by the project name testapp [root@maven testdir] # cd testapp/ # will be generated under the current directory. Enter the generated directory [root@maven testapp] # ls # to view the generated files under the directory pom.xml src [root@maven testapp] # cat pom.xml # pom file parameter interpretation project: root This is the constraint information to add some root elements to Project: modelversion: specify the version number of the current maven model grouopId: should be the name of the company or organization. Generally speaking, groupID consists of three parts, each of which is divided by "." Separation, the first part is the purpose of the project, such as com for business, org for non-commercial for-profit organizations, the second part is the company name, such as tentxun/baidu/alibaba, and the third part is your project name artifactId: it can be thought of as the project name built by maven, for example, your project has subprojects. You can use "project name-subproject naming method packaging: specify the generated format (jar/war/rar/pom/ear) version: version number, SNAPSHOT means snapshot, indicating that the project is still under development and is an unstable version of name: the name of the project, the documentation generated by Maven uses url: the url,Maven of the project home page produces documents with dependencies tags: specify that the development build (jar package) JUnit is a unit test framework for the Java language.

Local repositories are also generated under the user host directory:

[root@Maven testdir] # ls ~ / .m2/repository/antlr asm commons-codec commons-collections commons-io commons-lang jdom net org4) Maven configuration file

The configuration file name for Maven is: setting.xml. It is stored in two places:

1) / usr/local/maven/conf/settings.xml # installation directory

2) ~ / .m2/settings.xml # user's home directory

Among them, the installation directory configuration is the global configuration, and the user directory configuration is called user configuration. If both exist, their content will be merged, and user-wide settings.xml will take precedence.

There is no configuration file in the default user directory, so you can copy maven the settings.xml in the installation directory to the user directory.

The common configuration modifications for settings.xml files are as follows:

1) modify the storage path of the local warehouse. The default value is ~ / .m2/repository can be changed to the desired directory in localrepository in settings.xml.

2) if the build server cannot be connected to the remote repository due to network failure or security problems, you need to set the offline attribute to true in offline mode. Default is false.

3) modify the central warehouse server, modify the notes in the configuration file, and add or modify the image address

4) set the proxy address, modify the proxy tag in the configuration file, define the proxy server, and realize the proxy of maven.

As the central warehouse server is foreign, if it is used in China, the download is relatively slow, so it is recommended for domestic users to use the Aliyun warehouse address.

5) Maven common command [root@maven testapp] # pwd # confirm that the current directory is compiled under the previously built project / tmp/testdir/testapp [root@maven testapp] # mvn compile # (in the process of compilation, you can see that Aliyun's warehouse is used) [INFO] BUILD SUCCESS # as long as you see the line in the last few lines of the output information, it indicates that the execution is successful # all commands are the same after As long as the last few lines have the above information, it means that the execution is successful [root@maven testapp] # ls # will see that a new directory will be generated under the directory targetpom.xml src target# target: packaged output directory, such as packaged jar or war files # target/classes: compile output directory [root@maven testapp] # mvn test # test command [root@maven testapp] # ls target/test-classes/com# target/test-classes: test compilation output directory [root@maven testapp] # mvn clean # cleanup command (that is, delete the compilation and test generated directories) [root@maven testapp] # ls # target directory is no longer in pom.xml src [root@maven testapp] # mvn package # package The command [root@maven testapp] # ls target/ # generates a jar package classes surefire-reportsmaven-archiver testapp-1.0-SNAPSHOT.jarmaven-status test-classes [root@maven testapp] # mvn install # installation command (used to upload the compressed file jar package or war package to the local warehouse) [root@maven testapp] # ls ~ / .m2/repository/com/lzj/maven/quickstart/testapp/1.0-SNAPSHOT/ # View the uploaded package maven-metadata-local.xml testapp-1.0-SNAPSHOT.jar_remote.repositories testapp-1.0-SNAPSHOT.pom# directly use the maven install command to upload the project to the local warehouse # then all the previous steps will be performed automatically, such as compiling and packaging the source code, etc. [root@maven testapp] # mvn deploy # release (upload the package to the private server, which I do not have here, so it will fail) 6) the life cycle of the mvn command

Maven is based on the central concept of build lifecycle, which means that the process of building and distributing specific projects is clearly defined as follows:

Approximate life cycle diagram:

Detailed life cycle diagram:

7) build a web project and type out the war package And deploy to tomcat [root @ maven testdir] # mvn archetype:generate-DgroupId=cn.test.testweb-DartifactId=testweb-DarchetypeArtifactId=maven-archetype-webapp-DinteractiveMode=false# to build the web project # the generated directory structure is as follows: [root@maven testdir] # ls testweb/pom.xml src [root@maven testdir] # cat testweb/src/main/webapp/index.jsp Hello World! [root@maven testdir] # cd testweb/ # enter [root@maven testweb] # mvn package # under the project directory Package as war package [root@maven testweb] # ls target/ # will generate a war package under this directory classes maven-archiver testweb testweb.war# will move the generated war package to the web page root (webapps) of Tomcat It will be decompressed automatically. # then you can access the war package generated by the Tomcat test [root@maven testweb] # wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.31/bin/apache-tomcat-9.0.31.tar.gz[root@maven testweb] # tar zxf apache-tomcat-9.0.31.tar.gz [root@maven testweb] # mv apache-tomcat-9.0.31 / usr/local/tomcat # install tomcat [ Root@maven testweb] # mv target/testweb.war / usr/local/tomcat/webapps/ # move to the root directory of the Tomcat page [root@maven testweb] # / usr/local/tomcat/bin/startup.sh # start Tomcat [root @ maven testweb] # ls / usr/local/tomcat/webapps/ # View The war package is automatically decompressed, docs host-manager ROOT testweb.warexamples manager testweb.

The client accesses the war package moved to the root of the Tomcat web page, as shown in the figure:

The above is the installation and deployment of Maven method introduction, detailed use of their own use to know the specific points. If you want to read more related articles, you are welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report