Maven profile说明

版权声明:转载请标明出处,谢谢~ https://blog.csdn.net/wwtqq/article/details/81194048

最近使用maven 的profile功能来构建动态的maven项目,在打war包的时候遇到一个问题:
pom.xml中配置的profile内容无法打入war包。

上代码:

<?xml version="1.0" encoding="UTF-8"?>
<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>
  <parent>
        <artifactId>xxx</artifactId>
        <groupId>xxxx</groupId>
        <version>0.0.1-RELEASE</version>
    </parent>

  <artifactId>xxxxx</artifactId>
  <packaging>war</packaging>

  <name>heixia-webapp Maven Webapp</name>
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
        <dependency>
            <groupId>xxx</groupId>
            <artifactId>xxxx</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.nutz</groupId>
            <artifactId>nutz</artifactId>
            <version>1.r.66</version>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>ROOT</finalName>

        <resources>
            <!-- <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource> -->
            <resource>
                <directory>src/main/resources/${profiles.target}</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.8.v20171121</version>
                <configuration>
                    <path>/</path>
                    <jvmArgs>-Dfile.encoding=UTF-8</jvmArgs>
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.eclipse.jetty.websocket</groupId>
                        <artifactId>websocket-server</artifactId>
                        <version>9.4.8.v20171121</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://localhost:8989/manager/text</url>
                    <username>tomcat</username>
                    <password>tomcat</password>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>nutz</id>
            <url>https://jfrog.nutz.cn/artifactory/jcenter</url>
        </repository>
        <repository>
            <id>nutz-snapshots</id>
            <url>https://jfrog.nutz.cn/artifactory/snapshots</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <profiles>
        <profile>  //===此处该jar包始终无法打入war包中====//
            <id>intf</id>
            <dependencies>
                <dependency>
                    <groupId>com.heixia</groupId>
                    <artifactId>heixia-intf</artifactId>
                    <version>${project.version}</version>
                </dependency>
            </dependencies>
        </profile>
        <profile>  //===此处该jar包始终无法打入war包中====//
            <id>shortvideo</id>
            <dependencies>
                <dependency>
                    <groupId>com.heixia</groupId>
                    <artifactId>heixia-shortvideo</artifactId>
                    <version>${project.version}</version>
                </dependency>
            </dependencies>
        </profile>
    </profiles>

</project>

问题说明:
使用myeclipse10来启动web项目,如果希望web项目加载某个profile,直接在web项目上–>右键–>maven–>select profiles就可以自动加载并成功启动。
但是我在将这个web项目打包时始终无法将profile打入war包中。

摸索了半天终于解决了:
myeclipse10开发工具可以通过选择来自动加载,但是只能局限于myeclipse10开发工具中使用。
如果需要将web项目打成war包,并将profile也打包加载进去。需要在package命令加上profile的激活命令,命令如下:
clean package -DskipTests(打包时略过测试) -Pintf
如果需要多个profile同时生效,即:讲多个profile同时打入war包,命令如下:
clean package -DskipTests(打包时略过测试) -Pid1,id2,id3

猜你喜欢

转载自blog.csdn.net/wwtqq/article/details/81194048