使用maven profile 指定配置打包

使用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
这里写图片描述
命令格式为: mvn package -P配置的节点名称
如:mvn package -Phangzhou
或者
这里写图片描述

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Maven是一种流行的项目构建工具,通过使用Maven,我们可以定义和管理项目的依赖关系,并在构建过程中执行各种插件。 当我们使用Maven进行项目构建时,可以使用指定profile来帮助我们完成特定的构建需求。Profile是一种Maven的特性,它允许我们定义一组配置元素,以根据特定的需求来激活或禁用某些构建步骤或插件。 若要使用指定profile进行打包,则需要在项目的pom.xml文件中进行配置。首先,在pom.xml文件中添加一个新的profile元素,可以指定profile的id、activation条件以及任何其他的配置元素。 在配置profile时,我们可以使用activation元素来定义何时激活这个profile。例如,可以根据环境变量、操作系统等条件来激活特定的profile。 接下来,我们需要在pom.xml文件中指定使用profile。可以通过在命令行中使用“-P”参数来指定。例如,可以使用以下命令来指定profile为"dev": mvn clean package -P dev 通过上述命令,Maven使用指定profile来执行打包操作。这样,在构建过程中,只有与指定profile相关联的插件和配置元素会被执行。其他未被激活的profile将被忽略。 需要注意的是,除了命令行,profile还可以通过其他方式进行指定,例如在IDE中进行配置或在Maven配置文件中进行设置。 总之,通过使用Mavenprofile功能,我们可以根据特定需求来选择合适的profile,并通过指定命令行参数来进行打包操作,以达到定制化构建的目的。 ### 回答2: 要在Maven打包指定profile,可以通过使用以下命令行参数来实现: ```shell mvn clean package -P profile-name ``` 在上述命令中,`profile-name`是你想要打包profile的名称。Maven将会加载并应用指定profile配置,并使用配置打包项目。 在项目的`pom.xml`文件中,可以定义多个不同的profile,并根据不同的需求进行配置。每个profile都有一个唯一的名称,并且可以通过以下格式在`<profiles>`元素下进行定义: ```xml <profiles> <profile> <id>profile-name</id> ... <!-- 配置信息 --> ... </profile> </profiles> ``` 在定义的profile中,可以包含任意数量的配置信息,如依赖、插件、环境变量等。这些配置信息将在打包命令中根据所选择的profile生效。 当运行`mvn clean package -P profile-name`命令时,Maven将会加载并应用指定profile配置,并自动应用相关的依赖和插件。最终,Maven将会根据配置完成项目的打包工作。 通过使用Mavenprofile功能,可以方便地根据不同的需求打包项目,提高开发和部署的灵活性。 ### 回答3: 要使用Maven打包指定Profile,首先需要在项目的pom.xml文件中配置Profile。在pom.xml文件中,可以使用<profiles>标签来定义不同的Profile。每个Profile可以有自己的id和相关配置。 首先,在pom.xml文件的<profiles>标签内,添加一个<profile>标签,设置Profile的id。比如,我们可以定义一个名为"prod"的Profile。在<profile>标签中,可以定义一些特定于该Profile的属性和配置。例如,可以设置不同的插件版本、依赖项、资源文件路径等。 然后,在<profiles>标签内的<profile>标签中,使用<activation>标签设置激活该Profile的条件。可以通过<activation>标签的条件设置来决定什么时候应该激活该Profile。比如,可以根据系统属性、环境变量、文件是否存在等条件来激活Profile。 接下来,在命令行中使用Maven命令来打包指定Profile。在命令行中,可以使用"-P"选项来激活特定的Profile。例如,使用以下命令来打包激活名为"prod"的Profile: mvn package -P prod 这样,Maven将会根据指定Profile进行打包。它将使用<profiles>标签内所定义的相关配置、插件版本、依赖项等,从而生成特定的构建输出。 总结起来,要使用Maven打包指定Profile,我们需要在pom.xml文件中定义Profile并设置相关配置,然后在命令行中使用"-P"选项来激活指定Profile。这样,Maven将会根据该Profile配置进行打包
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值