Deep understanding of Maven and applications (2): flexible construction

Source: Internet
Author: User

Deep understanding of Maven and applications (1): lifecycle and plug-ins

Exam official URL: http://maven.apache.org/guides/index.html

A good building system must be flexible enough to allow projects to be successfully built in different environments. To support the flexibility of building, Maven has three built-in features:Attribute, profile, and resource Filtering.

1. MAVEN attributes

Maven attributes are classified into six categories:
1. built-in attributes: for example, $ {basedir} indicates the project root folder, and $ {version} indicates the project version number.
2. Pom attributes: You can reference the values in the POM file. For example:
$ {Basedir} project root folder
$ {Project. Build. Directory}: Build a folder. The default value is target.
$ {Project. Build. outputdirectory}: Output Folder of the build process. The default value is target/classes.
$ {Project. Build. finalname} Name of the output. The default value is $ {project. artifactid}-$ {project. Version}
$ {Project. Packaging} packaging type. The default value is jar.
$ {Project. xxx} content of the random node of the current pom File
3. Define attributes by yourself: You can define Maven attributes under the <Properties> element of POM.
4. setting attributes: You can use the attributes starting with settings to reference the values of XML elements in settings. XML, for example, $ {settings. localrepository} points to the address of your local repository.
5. java system attributes: Maven can use the attributes of the current java system. For example, $ {user. Home} points to the user folder.
6. environment variable attributes: All environment variables can use attributes starting with Env. For example, $ {env. java_hoe }.

2. Resource Filtering

Resources: All files under the src/main/resources and src/test/resources files. By default, these files will be copied to the following classpath, that is, target/classes or lower.
The so-called resource filtering means to filter the content in the files below these directories and check whether Maven variables need to be replaced. By default, only variables in POM. XML are replaced, and resource files are not filtered. However, they can be set, for example:

<Build> <finalname> agentmanager </finalname> <sourcedirectory> src/main/Java </sourcedirectory> <resources> <! -- Control the copying of resource files --> <resource> <directory> src/main/resources </directory> <excludes> <exclude> **/jre.zip </exclude> <exclude> **/jre.tar </exclude> <exclude> agentmanager. jsmooth </exclude> <exclude> assembly. XML </exclude> </Excludes> <targetpath >$ {project. build. directory }</targetpath> </resource> <directory> src/main/resources/conf </directory> <targetpath >$ {basedir}/conf </targetpath> <filtering> true </filtering> </resource> </resources> </build>


 

Such as JDBC. Properties

jdbc.driverClassName=${db.driver}jdbc.url=${db.url}jdbc.username=${db.user}jdbc.password=${db.pwd}

 

Profile file

<profiles> <profile>  <id>dev</id>  <properties>   <db.driver>oracle.jdbc.driver.OracleDriver</db.driver>   <db.url>jdbc:oracle:thin:@10.252.48.3:1521:dbname</db.url>   <db.user>username</db.user>   <db.pwd>userpwd</db.pwd>  </properties> </profile> <profile>  <id>test</id>  <properties>   <db.driver>oracle.jdbc.driver.OracleDriver</db.driver>   <db.url>jdbc:oracle:thin:@10.252.48.3:1521:testdbname</db.url>   <db.user>testusername</db.user>   <db.pwd>testuserpwd</db.pwd>  </properties> </profile></profiles>



One or more profiles can be activated using the-P limit number during construction, separated by commas (,).
For exampleMVN clean install-pdev

3. MAVEN Profile

The above example should show what profile is, in fact, it is equivalent to defining a series of profile variables, you can use a specific profile to replace the resource file in the detailed build.
There are many ways to activate the profile, such as command line activation (above), explicit activation of the settings file, activation of System Properties, activation of the operating system environment, default activation, and activation of files, detailed information on the official website.


3.1 profile types

According to the need, the profile can be declared in the following file.
1,Pom. xmlFor the current project
2,User settings. xmlThe. m2/settings. xml file in the user folder is valid for all projects of the current user.
3,Global settings. xmlConf/settings. XML in the maven installation folder. It is valid for all projects on this machine.

4. Web Resource Filtering

In the maven web project, in addition to the above-mentioned resource files (src/main/resources), another type is called the Web Resource folder, that isSrc/main/webappThe following JS, CSS, and so on. By default, these folders are not filtered by resources. The command to enable these folders is as follows:

  <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>2.1.1</version><configuration>    <webResources><resource><directory>src/main/webapp</directory>    <filtering>true</filtering>    <includes><include>**/*.css</include><include>**/*.js</include>    </includes>                          </resource>        </webResources></configuration></plugin>


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.