A good build system must be flexible enough to allow projects to be built successfully in different environments. In order to support the flexibility of build, MAVEN has built in three major features: attribute, profile, and resource filtering .
1. Maven Properties
The Maven attribute is divided into 6 categories:
1, built-in properties: such as ${basedir} represents the project root directory, ${version} represents the project version
2, POM properties: The user can refer to the corresponding value in the Pom file. Such as:
${BASEDIR} Project root directory
${project.build.directory} build directory, default to target
${project.build.outputdirectory} build process output directory, default to Target/classes
${project.build.finalname} output name, default is ${project.artifactid}-${project.version}
${project.packaging} package type, default to Jar
${PROJECT.XXX} contents of any node of the current Pom file
3. Custom properties: The user can customize the MAVEN properties under the <properties> element of the Pom.
4. Setting property: The user can use a property beginning with settings to reference the value of an XML element in Settings.xml, such as ${settings.localrepository} to the address of the user's local warehouse.
5. Java System Properties: Maven can use the properties of the current Java system, such as ${user.home}, to point to the user directory.
6. Environment variable Properties: All environment variables can be used with Env. Properties that begin with. such as: ${env. Java_hoe}.
2. Resource filtering
Here the so-called resources: also refers to the src/main/resources and src/test/resources files under all the files, by default, these files will be copied to classpath below, that is, under the target/classes.
The so-called resource filtering is to filter the contents of the files under these folders to see if the MAVEN variable needs to be replaced. By default, only the variables inside the pom.xml are replaced, the resource files are not filtered, but can be set as follows:
[HTML]View Plaincopy print?
- <build>
- <finalname>agentmanager</finalname>
- <sourcedirectory>src/main/java</sourcedirectory>
- <resources>
- <!--control a copy of the resource file--
- <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>
- <resource>
- <directory>src/main/resources/conf</directory>
- <targetpath>${basedir}/conf</targetpath>
- <filtering>true</filtering>
- </Resource>
- </Resources>
- </Build>
such as Jdbc.properties
[CSS]View Plaincopy print?
- Jdbc.driverclassname=${db.driver}
- Jdbc. url=${db. URL}
- Jdbc.username=${db.user}
- JDBC.PASSWORD=${DB.PWD}
Profile file
[HTML]View Plaincopy print?
- <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 at build time, separated by commas
such as mvn clean Install-pdev
3. Maven Profile
The above example should be able to see what the profile is doing, in fact, it is equivalent to define a series of profiles, in the concrete construction can use one of them to replace the resource file.
There are many ways to activate profile, such as command line activation (above), settings file explicit activation, System attribute activation, operating system environment activation, default activation, file presence or not activation, etc., can refer to official website information .
Types of 3.1 profiles
Depending on your needs, you can declare profile in the following file.
1,Pom.xml for the current project
2. user settings.xml . M2/settings.xml in the user directory, valid for all items of the current user.
3. The global settings.xml is the Conf/settings.xml under the MAVEN installation directory. Valid for all items on this computer.
4. Web Resource filtering
In the MAVEN Web project, in addition to the above mentioned resource files (src/main/resources), there is a class called Web Resources directory, that is, Src/main/webapp below the JS, CSS and so on. By default, these directories are not filtered by resources, and the commands that are opened are as follows:
[HTML]View Plaincopy print?
- <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>
Maven Combat (11) _ Upload widget to Nexus