Maven Best Practices

Source: Internet
Author: User
Document directory
  • Basic Configuration
  • Dependency Configuration

This article does not want to discuss what Maven is, what it can do, and what benefits we will have when selecting Maven ...... This article only provides a case, practice, and idea for your reference in the company's large-scale development environment. Is it "best "? Of course not! Because there is no "best", only "better ":-)

Pom, I have to mention it.

For Maven, the POM (poject object, project object) file is the whole of a project, and no other configuration files are needed to describe the project! When executing MVN in CMD/shell, package or other operations on the modified project, you only need to CD to the directory where the POM file is located. Of course, the premise is that you need to configure mvn in the system path. Pom Structure
    The pom file usually contains only five parts. We recommend that you configure it in this order:
    1. Basic Configuration
    2. Reference root-Pom-this is very important and will be explained later
    3. Developer information-"Orangutan" has been working hard for a long time. Keep a name.
    4. Build configuration-including necessary plug-in configuration
    5. Dependency configuration-the core of the core, many people are confused here when using maven. This article will introduce how to fix it on Root-Pom.

    In addition to the preceding five parts, you can add one <modules> node to the project. Other nodes do not have a strong reason. Therefore, we recommend that you increase the number with caution.

    Overall example

    <Project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <br/> <! -- Basic configuration --> <br/> <modelversion> 4.0.0 </modelversion> <br/> <groupid> COM. sample </groupid> <br/> <artifactid> sample-commons-MVC </artifactid> <br/> <packaging> jar </packaging> <br/> <version> 1.0-Snapshot </version> <br/> <Name> sample-commons-MVC </Name> <br/> <! -- Reference root pom --> <br/> <parent> <br/> <groupid> COM. sample </groupid> <br/> <artifactid> sample-root-Pom </artifactid> <br/> <version> 1.0-Snapshot </version> <br/> </parent> <br/> <! -- Developer --> <br/> <developers> <br/> <developer> <br/> <Name> zhangsan </Name> <br/> <email> zhangsan@sample.com </ email> <br/> </developer> <br/> </developers> </P> <p> <! -- Build configuration (only add the project-specific configuration) --> <br/> <build> <br/> <plugins> <br/> <plugin> <br/> <groupid> Org. apache. maven. plugins </groupid> <br/> <artifactid> Maven-jar-plugin </artifactid> <br/> <configuration> <br/> <archive> <br/> <manifestentries> <br/> <rose> applicationcontext </rose> <br/> </manifestentries> <br/> </archive> <br/> </configuration> <br /> </plugin> <br/> </plugins> <br/> </build> <br/> <! -- Dependency configuration --> <br/> <dependencies> <br/> <! -- Third-party dependency. You do not need to specify the version number, no need to write scope --> <br/> <dependency> <br/> <groupid> JUnit </groupid> <br/> <artifactid> JUnit </artifactid> <br/> </dependency> <br/> <groupid> javax. servlet </groupid> <br/> <artifactid> servlet-API </artifactid> <br/> </dependency> <br/> <groupid> net. user </groupid> <br/> <artifactid> User-API </artifactid> <br/> </dependency> <br/> <! -- For dependencies within an organization, you must specify the version number and possible scope --> <br/> <dependency> <br/> <groupid> COM. sample </groupid> <br/> <artifactid> sample-core </artifactid> <br/> <version> 1.0-Snapshot </version> <br/> </dependency> <br/> </dependencies> <br/> </Project>

    Basic Configuration

    <Modelversion> 4.0.0 </modelversion> <br/> <groupid> COM. sample </groupid> <br/> <artifactid> sample-xxx-yyy </artifactid> <br/> <version> 1.0-Snapshot </version> <br/> <packaging> JAR/war/POM </packaging> <br/> <Name> sample1/sample2-xxx-yyy </Name> <br/> <URL> http://repos.sample.com </URL>

    Note:

    Artifactid

    Sample-xxx-yyy, that is, main project name-first function name-second function description
    Version

    We recommend that you use a fixed version, for example, 1.0-snapshot.
    Packaging

    As shown in the preceding figure, select 1 from 3. Jar

    OrWar

    , Some basic root-POM can bePOM

    Name

    There are no special cases, same as artifactid
    Reference root POM

    <Parent> <br/> <groupid> COM. sample </groupid> <br/> <artifactid> sample-root-Pom </artifactid> <br/> <version> 1.0-Snapshot </version> <br/> </Paren

    • This is very important: the common configurations of each project can be uniformly attributed to this root-Pom, such as the version number of a third-party library and the required organization (the second party) dependent library ...... This can be effectiveAvoid confusion of dependent library versions

      Well design this

      Root-POM

      Based on the complexity of the overall plan, you can still

      Root-POM

      Then layer, for example:

    Pom defined by the Basic third-party library version <Project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <br/> <modelversion> 4.0.0 </modelversion> <br/> <groupid> COM. sample </groupid> <br/> <artifactid> sample-3rd-commons-versions </artifactid> <br/> <version> 1.0-Snapshot </version> <br/> <packaging> pom </packaging> <br/> <Name> sample-3rd-commons-versions </Name> <br/> <URL> http://maven.apache.org </URL> <br/> <Properties> <br/> <JUnit -version> 4.6 </JUnit-version> <br/> <spring-Mock-version> 2.0.8 </spring-Mock-version> <br/> <El-API-version> 1.0 </El-API-version> <br/> <activation-version> 1.1 </activation-version> <br/> <aopalliance-version> 1.0 </aopalliance- version> <br/> <servlet-API-version> 2.4 </servlet-API-version> <br/> <JSP-API-version> 2.0 </JSP-API- version> <br/>...

    Basic third-party library POM <Project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <br/> <modelversion> 4.0.0 </modelversion> <br/> <parent> <br/> <groupid> COM. sample </groupid> <br/> <artifactid> sample-3rd-commons-versions </artifactid> <br/> <version> 1.0-Snapshot </version> <br/> </parent> <br/> <groupid> COM. sample </groupid> <br/> <artifactid> sample-core </artifactid> <br/> <packaging> jar </packaging> <br/> <version> 1.0- snapshot </version> <br/> <dependencies> <br/> <dependency> <br/> <groupid> JUnit </groupid> <br/> <artifactid> JUnit </artifactid> <br/> <version >$ {JUnit-version} </version> <br/> <scope> test </scope> <br/> </dependency> <br/> <dependency> <br/> <groupid> commons-logging </groupid> <br/> <artifactid> commons-logging </artifactid> <br/> <version >$ {commons-logging-version} </version> <br/> </dependency> <br/>...Developer

    • Simply write the name information of the owner and main participants, and others can view the author through pom.
    <Developers> <br/> <developer> <br/> <Name> Zhang San </Name> <br/> <email> san.zhang@sample.com </Email> <br/> </ developer> <br/> </developers>Build configurations

    • You can configure some common plug-ins in the corresponding root-Pom, such:

      • Specifies that the compiled source file uses UTF-8 encoding.
      • Compile only source files in src/main/Java
      • Pack the XML, properties, and java files in src/main/Java into jar.
      • ......
    • A project exclusive. For example, to add an attribute to the manifest file, you should configure it separately in the POM of the project:

    <Build> <br/> <plugins> <br/> <plugin> <br/> <groupid> Org. apache. maven. plugins </groupid> <br/> <artifactid> Maven-jar-plugin </artifactid> <br/> <configuration> <br/> <archive> <br/> <manifestentries> <br/> <Name> value </Name> <br/> </manifestentries> <br/> </archive> <br/> </configuration> <br /> </plugin> <br/> </plugins> <br/> </build> <br/>Dependency Configuration

    The dependency on a third party should be different from that on a second party in the Organization.

    • For third-party dependencies, do not write the version number or scope, because this should be specified in the corresponding root-Pom:

    <Dependencies> <br/> <dependency> <br/> <groupid> JUnit </groupid> <br/> <artifactid> JUnit </artifactid> <br/> </ dependency> <br/> <dependency> <br/> <groupid> javax. servlet </groupid> <br/> <artifactid> servlet-API </artifactid> <br/> </dependency> <br/> <groupid> net. user </groupid> <br/> <artifactid> User-API </artifactid> <br/> </dependency> <br/>... <br/>

    • We recommend that you use a fixed version number (1.0-Snapshot) for dependency within the organization, which reduces the trouble of collaboration:

    ... <Br/> <dependency> <br/> <groupid> COM. sample </groupid> <br/> <artifactid> sample-core </artifactid> <br/> <version> 1.0-Snapshot </version> <br/> </dependency> <br/> <dependency> <br/> <groupid> COM. sample. xxx </groupid> <br/> <artifactid> XXX-adapter </artifactid> <br/> <version> 1.0-Snapshot </version> <br/> </dependency> <br/> <dependency> <br/> <groupid> COM. sample. CCC </groupid> <br/> <artifactid> CCC-util </artifactid> <br/> <version> 1.0-Snapshot </version> <br/> </dependency> <br/> </dependencies>Some other suggestions

    1. <Distributionmanagement> and <repository> configurations are defined in the appropriate root-Pom.
    2. Configure Maven's default structure, build/resources/resouce, and other configuration items in the corresponding root-Pom. It can also include medium details such as. SVN files that won't be hit by jar.
    3. Configure defaultgoal in the corresponding root-Pom. It is recommended to install, that is, $ MVN is equivalent to $ MVN install.
    Are there any better practices? Hope you can tell me :-)

    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.