In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use the Maven command". In daily operation, I believe many people have doubts about how to use the Maven command. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the Maven command". Next, please follow the editor to study!
Quickly create a Maven project Maven Archetype introduction
How to quickly create a Maven project can be created through Maven's Archetype. Archetype is a variety of engineering templates provided by Maven, through which different Maven project structures can be generated.
The list of Archetype provided by Maven is as follows:
Archetype ArtifactIdsDescriptionmaven-archetype-archetypegenerate a sample archetype project.maven-archetype-j2ee-simplegenerate a simplifed sample J2EE application.maven-archetype-mojogenerate a sample a sample Maven plugin.maven-archetype-plugingenerate a sample Maven plugin.maven-archetype-plugin-sitegenerate a sample Maven plugin site.maven-archetype-portletgenerate a sample JSR-268 Portlet.maven-archetype-quickstartgenerate a sample Maven project.maven-archetype-simplegenerate a simple Maven project.maven-archetype-sitegenerate a sample Maven site which demonstrates some of the supported document types like APT, XDoc And FML and demonstrates how to i18n your site.maven-archetype-site-simplegenerate a sample Maven site.maven-archetype-webappgenerate a sample Maven Webapp project.
Here we choose maven-archetype-quickstart as a template for quick creation because it provides a standard project structure based on which subsequent extensions can be made.
Project |-- pom.xml`-- src |-- main | `--java |`-- $package | `--App.java`-- test `--java`-- $package `--AppTest.java generates the Maven project
1) through the Maven command
Maven provides commands to quickly create Maven projects:
Mvn archetype:generate-DarchetypeGroupId=org.apache.maven.archetypes-DarchetypeArtifactId=maven-archetype-quickstart-DarchetypeVersion=1.4
The command specifies the Maven Archetype:maven-archetype-quickstart information we mentioned earlier.
During the run, you will be prompted to enter the Naven coordinate information of the project until the creation is complete. The basic part is not written here, and you need to follow the official account Java technology stack of the complete Maven tutorial to reply "mvn" in the background.
2) through IDEs
You can also quickly create Maven projects through IDE. Now Java IDEs supports Maven or comes with its own Maven plug-in. Take Intellij IDEA as an example to create.
Select maven-archetype-quickstart:
Enter the Naven coordinate information for the project:
Select Maven and warehouse setting information:
Wait for the project to complete, as shown below:
Maven Project ReFactor Maven Project configuration
After the project is generated, we can move the files from the original system to the new Maven project. Because the generated project structure is relatively simple, the maven-archetype-webapp template provided by Maven does not meet the requirements:
The nature of our project is also a background system, which involves all aspects, so we need to improve the creation of other resource directories:
-src-main-java-resources-filters-webapp-test-java-resources
This is a relatively standard Maven Web project structure, I made it into a basic scaffolding, but also integrated a variety of off-the-shelf plug-ins and functions, other projects to Maven can be directly applied.
Has been uploaded to Github:
Https://github.com/javastacks/maven-demo-project
Introduction to the main directory structure:
Directory description src/main/java source code directory src/main/resources resource directory src/main/filters multi-environment configuration filter directory src/main/webappWeb application file directory src/test/java test code directory src/test/resources test resource directory
After all the directories have been created, it is not difficult to move all the (* .jar) files of the original system to the corresponding directories according to the nature of the files.
Configure Maven environment information:
Src/main/filters/filter-$ {env} .properties src/main/resources/config true src/main/resources/resource false Org.apache.maven.plugins maven-compiler-plugin ${maven-compiler-plugin.version} org.apache.maven.plugins maven-war-plugin ${maven-war-plugin.version} false dev true Dev test test mirror mirror Prod prod Maven dependent transformation
Dependency conversion is to convert all the (* .jar) dependency packages of the original system into Maven dependency management. Here is the difficulty, to solve jar package conflicts, version conflicts and other exceptions encountered during compilation, startup and run.
My idea is to first transform the dependencies of some core frameworks, then some more independent public toolkits, and finally some unfamiliar dependency transformations.
When introducing Maven dependencies, take a look at all its dependent dependencies, and then gradually remove them from the lib directory until all of them are deleted and the conversion is complete.
When looking for corresponding dependencies, if the central warehouse cannot be found and other remote warehouses can find it, add the remote warehouse agent configuration where the package is located in the company's private repository.
If it cannot be found in a central warehouse or other remote warehouse, such as a third-party SDK package, it will be uploaded to the company's private repository. How to upload to the private library, click here to read more Maven tutorials in the official account Java technology stack reply maven to read.
When looking for Maven dependencies according to the JAR package, there may be multiple dependencies with the same name, and you don't know which one to refer to. You need to look at the package name in the original JAR package, and you will probably know the coordinate information according to the package name.
Dependency transfer:
In the process of dependency transformation, if one dependency depends on another, you can directly refer to the parent dependency, such as poi, poi-ooxm and poi-ooxml-schemas, which exist in the old project:
However, in the POI dependency system, the poi-ooxml package needs to rely on the other two packages, so you only need to introduce the poi-ooxml dependency:
Org.apache.poi poi-ooxml 4.1.2
This brings in all the other packages that the package needs to rely on, which is the advantage of Maven managing dependencies, avoiding multiple packages and fewer packages, and avoiding dependency conflicts as far as possible.
Scope of dependency:
In the old project, all jar packages are in the web-inf/lib directory, such as: Servlet, JUnit, these packages are still in the directory after the production package, there is no life cycle management.
Their lifecycle can be controlled in Maven:
Javax.servlet javax.servlet-api ${javax.servlet-api.version} provided junit junit ${junit.version} test
Servlet only needs to be used at compile time, and JUnit only needs to be used during testing, so as to control the dependency scope of each package and minimize the scope of each package.
Resolve conflicts:
Learn to use exclusion when passed dependent versions do not match, or when different versions of the same package cause conflicts:
Or force the specified package version:
Org.bouncycastle bcprov-jdk16 ${bctsp-jdk16.version}
Sometimes, use classifier to specify different versions of JDK as needed:
Com.test test ${test.version} JDK6
If the source code compilation error, do not know which JAR package, or which version, you can go to the original project point corresponding to the class reference to see.
The whole transformation process is relatively smooth, that is, some problems caused by JAR package conflicts need to be solved when compiling and running, according to the above methods until compilation and startup are normal.
At this point, the study on "how to use the Maven command" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.