【问题标题】:Maven module order changes on the same profile同一配置文件上的 Maven 模块顺序更改
【发布时间】:2017-07-19 11:23:31
【问题描述】:

我的项目有 3 个 pom 文件和 2 个 maven 配置文件:prod 和 dev。问题是当我使用 cmd 运行我的开发配置文件时

mvn clean install -Pdev 

它按顺序构建项目:

  1. 后端,
  2. 前端

当我使用 prod 配置文件构建项目时,它会按顺序构建项目:

  1. 前端
  2. 后端

这是我想要的方式。但是当我运行 cmd

mvn clean install

它需要使用开发配置文件构建项目,并且它按照以下顺序进行:1.frontend,2.backend。

这就是问题所在,它改变了模块顺序。这是主要的 pom.xml

<groupId>com.main</groupId>
<artifactId>Main</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Main</name>
<description>Main Parent Project</description>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<modules>
    <module>backend</module>
    <module>frontend</module>
</modules>

这里是后端 pom.xml

 <artifactId>backend</artifactId>
<name>backend</name>
<description>Backend Project</description>



<parent>
    <groupId>com.main</groupId>
    <artifactId>Main</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>



<dependencies>
 ...
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
                <profiles>${spring-profiles}</profiles>
            </configuration>
        </plugin>
    </plugins>
</build>
<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <build.profile.id>dev</build.profile.id>
            <profileActive>dev</profileActive>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <build.profile.id>prod</build.profile.id>
            <profileActive>prod</profileActive>
        </properties>
            <dependencies>
                <dependency>
                    <groupId>com.main</groupId>
                    <artifactId>frontend</artifactId>
                    <version>${project.version}</version>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
    </profile>
</profiles>

这里是前端 pom.xml

   <artifactId>frontend</artifactId>
<name>frontend</name>
<description>Frontend Project</description>

<parent>
    <groupId>com.main</groupId>
    <artifactId>Main</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<build>
    <plugins>
        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <workingDirectory>src/main/webapp</workingDirectory>
                <nodeVersion>v6.0.0</nodeVersion>
                <npmVersion>2.7.1</npmVersion>
                <nodeDownloadRoot>https://nodejs.org/dist/</nodeDownloadRoot>
                <npmDownloadRoot>https://registry.npmjs.org/npm/-/</npmDownloadRoot>
            </configuration>
            <executions>
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                    <phase>generate-resources</phase>
                </execution>
                <execution>
                    <id>npm install</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>install</arguments>
                    </configuration>
                </execution>
                <execution>
                    <id>npm rebuild node-sass</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>

                    <configuration>
                        <arguments>rebuild node-sass</arguments>
                    </configuration>
                </execution>
                <execution>
                    <id>bower install</id>
                    <goals>
                        <goal>bower</goal>
                    </goals>
                    <configuration>
                        <arguments>install</arguments>
                    </configuration>
                </execution>
                <execution>
                    <id>gulp</id>
                    <goals>
                        <goal>gulp</goal>
                    </goals>
                    <phase>generate-resources</phase>
                    <configuration>

                            <arguments>${gulpProfile}</arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>
<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <build.profile.id>dev</build.profile.id>
            <profileActive>dev</profileActive>
            <gulpProfile>-d</gulpProfile>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <build.profile.id>prod</build.profile.id>
            <profileActive>prod</profileActive>
            <gulpProfile>-p</gulpProfile>
        </properties>
    </profile>
</profiles>

你能帮我解释一下为什么会这样吗?

【问题讨论】:

    标签: maven build maven-profiles


    【解决方案1】:

    通常这种事情会得到解决,因为您的前端将依赖于您的后端生成的 jar。

    因此,即使它没有编译时依赖,您仍然可以在provided 范围内添加一个:

    <dependency>
       <groupId>${project.groupId}</groupId>
       <artifactId>backend</artifactId>
       <version>${project.version}</version>
       <scope>provided</scope>
    </dependency>
    

    这将告诉 maven reactor,它必须始终先构建后端,而不更改构建的结果工件。

    【讨论】:

      【解决方案2】:
      • 首先从 backend/pom.xml 中删除 &lt;activation&gt; 标签。我希望您默认一次只需要保持一个配置文件处于活动状态,并且应该是 dev
      • 从 prod 配置文件中删除 backend/pom.xml 中提到的依赖项。您的 fronend 项目应该依赖于 backend。
      • 在 frontend/pom.xml 中添加 backend 依赖项,如下所示(不在任何特定配置文件中)

      代码:

      <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>backend</artifactId>
        <version>${project.version}</version>
        <scope>provided</scope>
      </dependency>
      

      现在试试下面的命令

      mvn clean    // (default profile `dev`)
      mvn clean --activate-profiles prod     // (with override profile `prod`)
      

      【讨论】:

      • @Bohdan Levkovych,上述步骤是否帮助您解决了问题?
      猜你喜欢
      • 1970-01-01
      • 2016-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 2011-05-23
      相关资源
      最近更新 更多