maven profile具体项目中的使用

方法一

<profiles>
  <profile>
     <id>dev</id>
     <properties>
        <deploy.type>dev</deploy.type>
     </properties>
     <activation>
        <activeByDefault>true</activeByDefault>
     </activation>
   </profile>
   <profile>
     <id>test</id>
     <properties>
        <deploy.type>test</deploy.type>
     </properties>
     <activation>
        <activeByDefault>false</activeByDefault>
     </activation>
   </profile>
   <profile>
     <id>product</id>
      <properties>
        <deploy.type>product</deploy.type>
     </properties>
     <activation>
        <activeByDefault>false</activeByDefault>
     </activation>
   </profile>
</profiles>

<build>
    <finalName>portal</finalName>
    <resources>
          <resource>
             <directory>src/main/resources</directory>
             <excludes>
                <exclude>test/**</exclude>
                <exclude>product/**</exclude>
                <exclude>dev/**</exclude>
                <exclude>mybatis-generator/**</exclude>
             </excludes>
          </resource>
          <resource>
             <directory>src/main/resources/${deploy.type}</directory>
          </resource>
       </resources>

       <plugins>
        <!-- compiler插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <showWarnings>true</showWarnings>
            </configuration>
        </plugin>
       </plugins>
</build>

方法二

<profiles>
    <profile>
        <id>devtest</id>
        <build>
            <defaultGoal>package</defaultGoal>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <excludes>
                        <exclude>wisea.properties</exclude>
                        <exclude>log4j.xml</exclude>
                    </excludes>
                </resource>
                <resource>
                    <directory>src/devtest/resources</directory>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <configuration>
                        <target>
                            <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" />
                            <deploy url="http://192.168.20.208:8090/manager/text"
                                username="tomcatMa" password="tomcatMA13%" path="/nfsj.admin"
                                war="file:${project.build.directory}/${project.build.finalName}.${project.packaging}"
                                update="true" />
                        </target>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.apache.tomcat</groupId>
                            <artifactId>catalina-ant</artifactId>
                            <version>6.0.45</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>production</id>
        <build>
            <defaultGoal>package</defaultGoal>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <excludes>
                        <exclude>wisea.properties</exclude>
                        <exclude>log4j.xml</exclude>
                    </excludes>
                </resource>
                <resource>
                    <directory>src/production/resources</directory>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <webResources>
                            <resource>
                                <directory>src/production/webapp/</directory>
                                <targetPath>/</targetPath>
                                <includes>
                                    <include>static/js/common/wbf-init.js</include>
                                </includes>
                            </resource>
                            <resource>
                                <directory>src/main/webapp/</directory>
                                <targetPath>/</targetPath>
                                <excludes>
                                    <exclude>static/js/common/wbf-init.js</exclude>
                                </excludes>
                            </resource>
                        </webResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 IDEA 开发环境使用 Maven 项目时,可以使用 profile 来指定不同的环境。通常情况下,我们会为开发、测试和部署环境创建不同的 profile具体步骤如下: 1. 在 pom.xml 文件,添加 profile 标签。 ``` <profiles> <profile> <id>dev</id> <properties> <env>dev</env> </properties> </profile> <profile> <id>test</id> <properties> <env>test</env> </properties> </profile> <profile> <id>prod</id> <properties> <env>prod</env> </properties> </profile> </profiles> ``` 这里我们创建了三个 profile,分别对应着 dev、test 和 prod 环境。 2. 在 pom.xml 文件,添加 build 标签,并在其引用 profile。 ``` <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <executions> <execution> <id>default-copy-resources</id> <phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/classes</outputDirectory> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 这里我们使用 Maven 插件 maven-resources-plugin,将 src/main/resources 目录的资源文件复制到 target/classes 目录,并在复制的过程进行过滤,替换其的占位符。 3. 在 resources 目录,创建三个文件夹 dev、test 和 prod,并在其创建一个名为 application.properties 的文件。 ``` src/main/resources/ ├── dev │ └── application.properties ├── prod │ └── application.properties └── test └── application.properties ``` 这里我们为每个环境创建了一个 application.properties 文件,用于存放该环境的配置信息。 4. 在 application.properties 文件,添加一些配置信息。 ``` server.port=8080 spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=root ``` 这里我们添加了一些常用的配置信息。 5. 在 IDEA 开发环境,选择 Edit Configurations,在其添加一个 Maven 配置。 6. 在 Command line 输入以下命令: ``` clean install -Pdev ``` 这里的 -P 参数表示指定 profile,这里我们指定了 dev 环境。 7. 点击 OK,保存配置。 现在,我们可以在 dev 环境运行项目了。如果需要在其他环境运行项目,只需要更改 Command line profile 参数即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值