欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用maven profile 指定配置打包

程序员文章站 2024-02-07 22:01:22
...

使用maven profile 指定配置打包
1、配置profiles

 <profiles>
    <profile>
      <!-- 打包环境 -->
      <id>hangzhou</id>
      <properties>
        <!-- 节点名称 -->
        <environment>hangzhou</environment>
        <!--强制覆盖文件-->
        <maven.resources.overwrite>true</maven.resources.overwrite>
      </properties>
      <activation>
        <activeByDefault>true</activeByDefault><!-- 默认**该profile节点-->
      </activation>
    </profile>
    <profile>
      <!-- 节点名称 -->
      <id>guangzhou</id>
      <properties>
        <!-- 节点名称 -->
        <environment>guangzhou</environment>
        <!--强制覆盖文件-->
        <maven.resources.overwrite>true</maven.resources.overwrite>
      </properties>
    </profile>
    <profile>
      <!-- 节点名称 -->
      <id>haikou</id>
      <properties>
        <!-- 节点名称 -->
        <environment>haikou</environment>
        <maven.resources.overwrite>true</maven.resources.overwrite>
      </properties>
    </profile>
  </profiles>

2、配置resources

<resources>
      <resource>
        <directory>src/main/resources</directory>
        <excludes>
          <!--排出目录-->
          <exclude>buildconfig/hangzhou/*</exclude>
          <exclude>buildconfig/guangzhou/*</exclude>
          <exclude>buildconfig/haikouo/*</exclude>
        </excludes>
      </resource>
      <resource>
        <directory>src/main/resources/buildconfig/${environment}</directory>
        <!--<targetPath>/</targetPath>-->
        <!--不设置targetPath表示直接写入根目录-->
      </resource>
    </resources>

build文件具体代码


  <build>
    <finalName>signal-jam</finalName>

    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <excludes>
          <!--排出目录-->
          <exclude>buildconfig/hangzhou/*</exclude>
          <exclude>buildconfig/guangzhou/*</exclude>
          <exclude>buildconfig/haikouo/*</exclude>
        </excludes>
      </resource>
      <resource>
        <directory>src/main/resources/buildconfig/${environment}</directory>
        <!--<targetPath>/</targetPath>-->
        <!--不设置targetPath表示直接写入根目录-->
      </resource>
    </resources>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <configuration>
          <verbose>true</verbose>
          <overwrite>true</overwrite>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
          </dependency>
        </dependencies>
        <version>${mybatis-generator.version}</version>
      </plugin>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${spring-boot.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <!-- do not enable it, this will creates a non standard jar and cause autoconfig to fail -->
          <executable>false</executable>
          <mainClass>com.aliyun.citybrain.App</mainClass>
        </configuration>
      </plugin>

    </plugins>
  </build>

4、输入命令打包 或 idea maven侧边栏 的package
使用maven profile 指定配置打包
命令格式为: mvn package -P配置的节点名称
如:mvn package -Phangzhou
或者
使用maven profile 指定配置打包