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

Source: Internet
Author: User

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

Reference 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 directory, and $ {version} indicates the project version.
2. POM attributes: You can reference the values in the pom file. For example:
$ {Basedir} project root directory
$ {Project. build. directory} build directory. The default value is target.
$ {Project. build. outputDirectory}: output directory 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} contents of any node in the current pom File
3. Custom Attributes: You can customize 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, the address of $ {settings. localRepository} pointing to the user's local repository.
5. java system attributes: maven can use the attributes of the current java system. For example, $ {user. home} points to the user directory.
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 are copied to the classpath, that is, under target/classes.
The so-called resource filtering is to filter the content in the files under these folders and check whether maven variables need to be replaced. By default, only the variables in pom. xml are replaced, and resource files are not filtered. However, you can set them as follows:

<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>



You can use the-P parameter to activate one or more profiles during construction. Multiple profiles are separated by commas (,).
For exampleMvn clean install-Pdev

3. maven profile

In the above example, we can see what profile does. In fact, it is equivalent to defining a series of profile variables. You can use a specific profile to replace the resource file during a specific 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, for details, refer to the official website..


3.1 profile types

You can declare the profile in the following file as needed.
1,Pom. xmlFor the current project
2,User settings. xmlThe. m2/settings. xml file in the user directory is valid for all projects of the current user.
3,Global settings. xmlConf/settings. xml in the maven installation directory. 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), there is also a kind of directory called web resources, that isSrc/main/webappThe following js, css, and so on. By default, these directories are not filtered by resources. The command to enable these directories 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.