Maven basics 3

Source: Internet
Author: User
Tags xml parser sonatype

Instance:

1. Create a Maven project simple-weather

C:/> MVN archetype: Create-dgroupid = cn. vicky. maven. ch04-dartifactid = simple-weather-dpackagename = cn. vicky. maven-dversion = 1.0 (overwrite the default 1.0-Snapshot)

 

2. What we want to do is to add some information about the project license, organization, and project-related developers.

<Developers> <br/> <developer> <br/> <ID> Jason </ID> <br/> <Name> Jason Van Zyl </Name> <br/> <email> jason@maven.org </Email> <br/> <URL> http://www.sonatype.com </URL> <br/> <organization> sonatype </organization> <br/> <organizationurl> http://www.sonatype.com </organizationurl> <br/> <roles> <br/> <role> developer </role> <br/> </roles> <br/> <timezone>-6 </timezone> <br/> </developer> <br/> </developers>

 

3. Add new dependency

In this project, we will use dom4j and jaxen. In order to format the output of this command line program, we will use velocity. We also need to add dependencies on log4j for logs.

<Dependencies> <br/> <! -- Add a new library file --> <br/> <dependency> <br/> <groupid> log4j </groupid> <br/> <artifactid> log4j </artifactid> <br/> <version> 1.2.14 </version> <br/> </dependency> <br/> <groupid> dom4j </groupid> <br /> <artifactid> dom4j </artifactid> <br/> <version> 1.6.1 </version> <br/> </dependency> <br/> <dependency> <br/> <groupid> jaxen </groupid> <br/> <artifactid> jaxen </artifactid> <br/> <version> 1.1.1 </version> <br/> </dependency> <br/> <dependency> <br/> <groupid> velocity </groupid> <br/> <artifactid> velocity </artifactid> <br/> <version> 1.5 </version> <br/> </dependency> <br/> <groupid> JUnit </groupid> <br/> <artifactid> JUnit </artifactid> <br/> <version> 3.8.1 </version> <br/> <scope> test </scope> <br/> </dependency> <br/> </dependencies>

You can use the http://www.mvnrepository.com and then search for some common class libraries, such as hibernate or spring framework.

 

4. Run MVN install, so MAVEN will automatically download the library files!

5. Program writing

6. This project depends on two classpath resources: the main class configures log4j through the classpath resource log4j. preoperties. weatherformatter references a velocity template named output. VM in classpath. To add these resources, we need to create a new directory under the Basic directory of the project --
Src/main/resources. Because the directory is not created for the task archetype: Create, we need to create it by running the following command in the basic directory of the project:

$ CD src/main
$ Mkdir Resources
$ CD Resources

 

7. Run MVN install.

If a Maven error occurs (1.3 does not support generics)

Configure Pom. xml

<Build> <br/> <plugins> <br/> <plugin> <br/> <artifactid> Maven-compiler-plugin </artifactid> <br/> <configuration> <br/> <target> 1.5 </Target> <br/> <source> 1.5 </source> <br/> <encoding> UTF-8 </encoding> <br/> </configuration> <br/> </plugin> <br/> </plugins> <br/> </build>

Run the project MVN Exec: Java-dexec. mainclass = org. sonatype. mavenbook. Weather. Main

The exec plug-in allows us to run this program without loading appropriate dependencies to classpath.

 

8. Print project list

MVN dependency: Resolve

 

Com. IBM. ICU: icu4j: jar: 2.6.1: Compile
Commons-collections: commons-collections: jar: 3.1: Compile
Commons-Lang: commons-Lang: jar: 2.1: Compile
Dom4j: dom4j: jar: 1.6.1: Compile
Jaxen: jar: 1.1.1: Compile
JDOM: jar: 1.0: Compile
JUnit: jar: 3.8.1: Test
Log4j: log4j: jar: 1.2.14: Compile
Oro: jar: 2.0.8: Compile
Velocity: velocity: jar: 1.5: Compile
Xalan: jar: 2.6.0: Compile
Xerces: xercesimpl: jar: 2.6.2: Compile
Xerces: xmlparserapis: jar: 2.6.2: Compile
XML-APIs: jar: 1.0.b2: Compile
XOM: jar: 1.0: Compile

As you can see, our project has a large dependency set. Although we only introduced direct dependencies for the four class libraries, it seems that we actually introduced a total of 15 dependencies. Dom4j depends on xerces and XML Parser APIs. jaxen depends on xalan, which is also available in classpath.

 

9. View the detailed dependency tree.

In 8, we can see that four jar packages depend on the jar packages, but we cannot know that a specific jar package depends on those jar packages. If you want to know the entire dependency tree of your project, you can run the MVN dependency: Tree target.

CN. Vicky. Maven. ch04: simple-weather: jar: 1.0
+-Log4j: log4j: jar: 1.2.14: Compile
+-Dom4j: dom4j: jar: 1.6.1: Compile
|/-XML-APIs: jar: 1.0.b2: Compile
+-Jaxen: jar: 1.1.1: Compile
| +-JDOM: jar: 1.0: Compile
| +-Xerces: xercesimpl: jar: 2.6.2: Compile
|/-XOM: jar: 1.0: Compile
| +-Xerces: xmlparserapis: jar: 2.6.2: Compile
| +-Xalan: jar: 2.6.0: Compile
|/-Com. IBM. ICU: icu4j: jar: 2.6.1: Compile
+-Velocity: jar: 1.5: Compile
| +-Commons-collections: jar: 3.1: Compile
| +-Commons-Lang: jar: 2.1: Compile
|/-Oro: jar: 2.0.8: Compile
/-JUnit: jar: 3.8.1: Test

If you are not satisfied yet, or want to view the complete dependency trace, including those components that are not introduced due to conflicts or other reasons, open the maven debugging tag and run: MVN install-x

 

10. Write Unit Tests

Maven has built-in support for Unit Testing. Testing is part of the default Maven lifecycle.

This part is skipped ......

 

11. Add a test scope dependency

Some library files are only used for testing. Therefore, you can add range configurations for these library files. For example, <scope> test </scope>

 

12. Package and release a project

Maven Assembly plug-in is a plug-in used to create a distribution package specific to your application. To configure the maven Assembly plug-in, add the following plug-in configuration to the build configuration in POM. xml.

<Plugin> <br/> <artifactid> Maven-assembly-plugin </artifactid> <br/> <configuration> <br/> <descriptorrefs> <br/> <descriptorref> jar-with-dependencies </descriptorref> <br/> </descriptorrefs> <br/> </configuration> <br/> </plugin>

Run: MVN install assembly: Assembly build assembly

Now assembled in target/simple-weather-1.0-jar-with-dependencies.jar!

 

13. After assembling the project, we can run the project

CD target

C:/simple-weather/Target> JAVA-CP simple-weather-1.0-jar-with-dependencies.jar org. sonatype. mavenbook. Weather. Main 10002

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.