【问题标题】:Maven profile executionMaven 配置文件执行
【发布时间】:2013-12-20 05:59:08
【问题描述】:

我有一个问题,直到现在才确定原因。

我有一个包含多个模块的 Maven 项目。这些模块之一是 Web 服务客户端。

所以,在开发过程中,在maven中运行安装时,需要访问本地服务器来生成客户端。当我运行插件来生成项目的发布时,客户端应该指向生产服务器。

为此,我设置了一个关键属性 ${server.address},用于在生成客户端时指向服务器。有一个配置文件,当它处于活动状态时,此密钥属性会将地址重写到生产服务器。

发生了什么事?运行 mvn install 生成正确,即指向本地服务器。当我使用命令 mvn release:prepare -B release:perform -Denv=prd 生成发行版时,并没有按照应有的方式重写变量。

奇怪的是,如果我运行 mvn install -Denv=prd,它会正确生成,并指向生产服务器。

有人可以告诉我在发布周期中要更改哪些内容吗?

<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>

    <groupId>groupId</groupId>
    <artifactId>artifactId</artifactId>
    <version>0.0.2-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <server.address>http://localhost:8080</server.address>
    </properties>

    <profiles>
        <profile>
            <id>prd</id>
            <activation>
                <property>
                    <name>env</name>
                    <value>prd</value>
                </property>
            </activation>
            <properties>
                <server.address>http://srvprd009:8080</server.address>
            </properties>
          </profile>
    </profiles>

    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
        </testResources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-client</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <sourceRoot>src/main/gen</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>${server.address}/services/utilities?wsdl</wsdl>
                                    <extraargs>
                                        <extraarg>-p</extraarg>
                                        <extraarg>${project.package}</extraarg>
                                        <extraarg>-impl</extraarg>
                                        <extraarg>-verbose</extraarg>
                                        <extraarg>-frontend</extraarg>
                                        <extraarg>jaxws21</extraarg>
                                        <extraarg>-xjc-XhashCode</extraarg>
                                        <extraarg>-xjc-Xequals</extraarg>
                                    </extraargs>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.cxf</groupId>
                        <artifactId>cxf-rt-frontend-jaxws</artifactId>
                        <version>${cxf.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.cxf</groupId>
                        <artifactId>cxf-rt-transports-http</artifactId>
                        <version>${cxf.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>0.6.4</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>${maven-clean-plugin.version}</version>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>src/main/gen</directory>
                            <includes>
                                <include>**/*.*</include>
                            </includes>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>${build-helper-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/main/gen</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>ts-core</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>corporate-tools.fragmental.security</groupId>
            <artifactId>basic-ws-client</artifactId>
            <version>${fragmental.version}</version>
        </dependency>

        <!-- JEE -->
        <dependency>
            <groupId>javax.j2ee</groupId>
            <artifactId>j2ee</artifactId>
            <version>1.4</version>
            <scope>provided</scope>
        </dependency>

        <!-- JAX-WS -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>${jaxws.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.ws</groupId>
            <artifactId>jaxws-api</artifactId>
            <version>${jaxws.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.xml.soap</groupId>
                    <artifactId>saaj-api</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>jsr250-api</artifactId>
                    <groupId>javax.annotation</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</project>

【问题讨论】:

    标签: maven maven-profiles


    【解决方案1】:

    发布插件有点奇怪(对我来说;))使用特殊属性并将它们转发到执行的真正目标。如果您使用正常的配置文件参数 -Pprod 这将起作用。但是,您在 mvn 调用中添加的大多数其他参数都将被忽略。那你能不能试试下面这个:

    mvn release:prepare -B release:perform -Darguments="-Denv=prd" 
    

    【讨论】:

      【解决方案2】:

      我相信您使用的是 Maven 2.0.x 对吗?

      如果我没记错的话,配置文件中的属性覆盖在 Maven 2.0.7 或之前无法正常工作。升级到最新的 2.0.x (2.0.11) 甚至 2.2.x/3.0.x 将按您的预期工作。

      但是,有点跑题了,我相信您正在访问服务器以获取 WSDL 对吗?我认为这不是一个好主意,尤其是对于发布构建,因为它使构建不可重现。考虑将 WSDL 与源代码放在一起(至少对于发布构建而言)以使构建可重现。

      【讨论】:

        【解决方案3】:

        我修复了创建两个配置文件的问题,假设第一个默认激活 = true 并将我的 server.address 设置为 localhost。

        将server.address设置为prd服务器的第二个配置文件由命令行直接调用-P prd

        所以,当我不带参数执行时,默认配置文件将 server.address 设置为 localhost:8080,当我执行发布时,我使用 -P prd 并且发布工作正常。

        感谢您的回答。

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-07
        • 2016-04-07
        • 2023-03-12
        • 2020-04-04
        • 2020-06-03
        • 2013-07-15
        • 1970-01-01
        • 2015-12-14
        相关资源
        最近更新 更多