Maven configuration, dependent on third-party jar package packaged multi-environment configuration and Profiles

By the deployment of a package failure caused by in-depth exploration ┑ (¯ ▽ ¯) ┍

A, Maven configuration

1, Overview

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
 
  <!-- The Basics -->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <packaging>...</packaging>
  <dependencies>...</dependencies>
  <parent>...</parent>
  <dependencyManagement>...</dependencyManagement>
  <modules>...</modules>
  <properties>...</properties>
 
  <!-- Build Settings -->
  <build>...</build>
  <reporting>...</reporting>
 
  <!-- More Project Information -->
  <name>...</name>
  <description>...</description>
  <url>...</url>
  <inceptionYear>...</inceptionYear>
  <licenses>...</licenses>
  <organization>...</organization>
  <developers>...</developers>
  <contributors>...</contributors>
 
  <!-- Environment Settings -->
  <issueManagement>...</issueManagement>
  <ciManagement>...</ciManagement>
  <mailingLists>...</mailingLists>
  <scm>...</scm>
  <prerequisites>...</prerequisites>
  <repositories>...</repositories>
  <pluginRepositories>...</pluginRepositories>
  <distributionManagement>...</distributionManagement>
  <profiles>...</profiles>
</project>

2, the basic configuration

groupId + artifactId + version: the composition of the unique positioning of the project, when groupId and version are inherited from a parent, you do not need to explicitly define them;
Packaging: definitions are packaged, the current major packaging methods are pom, jar, maven-plugin, ejb, war, ear, rar, wherein the packaging is the default mode jar;
Properties: constants defined pom, pom pom constants can be anywhere in the file referenced by $ {};

3, build configuration

Build configuration is divided into two, "Project Build" and "Profile Build"

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      https://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <!-- "Project Build" contains more elements than just the BaseBuild set -->
  <build>...</build>
 
  <profiles>
    <profile>
      <!-- "Profile Build" contains a subset of "Project Build"s elements -->
      <build>...</build>
    </profile>
  </profiles>
</project>

build: directory structure and plug-in management to define the project;

<build>
  <defaultGoal>install</defaultGoal>
  <directory>${basedir}/target</directory>
  <finalName>${artifactId}-${version}</finalName>
  <filters>
    <filter>filters/filter1.properties</filter>
  </filters>
  ...
</build>

defaultGoal: specify the default targets or stage, such as the target may be a jar: jar, the install stage may be, these same two effects;
Directory: Construction of the target directory, the default is the basedir} {$ / target;
a finalName: project finally produced name, the default is artifactId} {$ - $ {Version};
Filters: define the needs of the application * .properties files. In other words, the filter is defined at build file "name = value" will replace the resource file $ {name} string. Maven directory is the default filter basedir} {$ / src / main / Filters;
Resources: resource list, the contents of the file associated with the project description and location

  <build>
    ...
    <resources>
      <resource>
        <targetPath>META-INF/plexus</targetPath>
        <filtering>false</filtering>
        <directory>${basedir}/src/main/plexus</directory>
        <includes>
          <include>configuration.xml</include>
        </includes>
        <excludes>
          <exclude>**/*.properties</exclude>
        </excludes>
      </resource>
    </resources>
    ...
  </build>

targetPath: After defining project generate resource file placement location, default location is the basedir} {$;
Filtering: Optional value of true or false, to define whether filtering is enabled for this resource. And filters, profile elements in combination;
directory: definitions find resource file in which the default location is $ {basedir} / src / main / Resources;
Includes: resource file definition directory need to include and support * as a wildcard match the file name;
excludes: includes the same structure, the resource file to be ignored define directory, includes and excludes when there is conflict, excludes prevail;

4, environment configuration

profiles: Change the settings depending on the build environment;

<profiles>
    <profile>
      <id>test</id>
      <activation>...</activation>
      <build>...</build>
      <modules>...</modules>
      <repositories>...</repositories>
      <pluginRepositories>...</pluginRepositories>
      <dependencies>...</dependencies>
      <reporting>...</reporting>
      <dependencyManagement>...</dependencyManagement>
      <distributionManagement>...</distributionManagement>
    </profile>
  </profiles>

activation: the current profile is described activated conditions, when the conditions inside described activation are satisfied, the current profile is activated;

<profiles>
    <profile>
      <id>test</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <jdk>1.5</jdk>
        <os>
          <name>Windows XP</name>
          <family>Windows</family>
          <arch>x86</arch>
          <version>5.1.2600</version>
        </os>
        <property>
          <name>sparrow-type</name>
          <value>African</value>
        </property>
        <file>
          <exists>${basedir}/file2.properties</exists>
          <missing>${basedir}/file1.properties</missing>
        </file>
      </activation>
      ...
    </profile>
  </profiles>

activeByDefault: whether it is activated by default;
jdk: jdk version number of the operating environment;
os: operating system of property operating environment;
File: Which files exist or what file is missing;

Second, third-party package dependent jar package

Maven projects have sometimes encountered in third-party cases rely packaged, if at this time do not want to deploy the Maven repository, you can do:
fight jar package
1, create a new directory for storing third-party dependency, the directory location depends on personal preference, here lib placed in the new project root directory;
2, POM file, add dependency;

<dependency>
        <groupId>xx</groupId>
        <artifactId>xx</artifactId>
        <version>2.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/xx-xx-2.0.jar</systemPath>
    </dependency>

3, add the resource configuration in the build element pom file, the purpose is to rely on third-party lib in the hit BOOT-INF / lib / down. Here still need to configure another resource, because it will be added to cover the front of this configuration out of the default resource configuration results in the original project to build resource files into the bag not generated;

 <resources>
        <resource>
            <directory>lib</directory>
            <targetPath>BOOT-INF/lib/</targetPath>
            <includes>
                <include>**/*.jar</include>
            </includes>
         </resource>
         <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
    </resources>

Playing war package
1 with step jar package, step 3 need to modify

<!--设置maven-war-plugins插件 -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
            <webResources>
                <resource>
                    <directory>lib</directory>
                    <targetPath>WEB-INF/lib/</targetPath>
                    <includes>
                        <include>**/*.jar</include>
                    </includes>
                </resource>
            </webResources>
        </configuration>
    </plugin>

Three, Profiles multi-environment configuration

1, create a new directory for storing filter configuration file, directory where the new filters, and there is localhost.properties, test.properties, prod.properties three environmental profile.
2, pom file, add configuration profiles

<profiles>
        <profile>
            <id>localhost</id>
            <properties>
                <profiles.active>localhost</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
    </profiles>

3, add pom configuration file in the build element

<resources>
    <resource>
        <directory>${basedir}/src/main/resources</directory>
        <includes>
            <include>**/*</include>
        </includes>
        <filtering>true</filtering>
    </resource>
</resources>
<filters>
    <filter>${basedir}/filters/${profiles.active}.properties</filter>
</filters>

4, in preparing the general profile / src / main under $ {basedir} / resources directory, using the configuration values ​​$ {name} placeholder (SpringBoot item using @ name @ placeholder) filters to obtain the corresponding profile in the environment

Reference article:
https://www.jianshu.com/p/574f74d1d0ee
http://maven.apache.org/pom.html

Guess you like

Origin www.cnblogs.com/irain/p/12030646.html