Maven 使用 mvn test 版本号时无法解析依赖关系

Posted

技术标签:

【中文标题】Maven 使用 mvn test 版本号时无法解析依赖关系【英文标题】:Maven could not resolve dependencies when using mvn test version number 【发布时间】:2019-12-24 16:55:10 【问题描述】:

你好,我有两个 spring boot 模块,一个 mediator 类和一个 mockserver 类。 mediator 类有父 pom,mockserver 类有子 pom。他们在各自的端口上都有两个 Spring Boot 应用程序。

我正在尝试为中介类的 mockserver 类 pom 添加一个依赖项,如下所示:

<dependency>
    <groupId>com.nulogix</groupId>
    <artifactId>billing_mediator</artifactId>
    <version>$nulogix-release-number-$git.version.number</version>
    <scope>compile</scope>
  </dependency>

当我运行 mvn test 时,虽然我得到了这个错误,但我不知道为什么,因为版本是从父 POM 复制的。

[ERROR] Failed to execute goal on project mock_server: Could not resolve dependencies for project com.nulogix:mock_server:jar:0.9.6_M2-$git.commit.time.$git.commit.id.describe-short: Failure to find com.nulogix:billing_mediator:jar:0.9.6-M2 

这是我的中介 pom 文件:

    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>com.nulogix</groupId>
    <artifactId>billing_mediator</artifactId>
    <version>$nulogix-release-number-$git.version.number</version>
    <packaging>pom</packaging>

    <name>billing</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven-jaxb2-plugin.version>0.14.0</maven-jaxb2-plugin.version>
        <jaxb2-specVersion>2.2</jaxb2-specVersion>
        <git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
        <nulogix-release-number>0.9.6_M2</nulogix-release-number>
        <git.version.number>$git.commit.time.$git.commit.id.describe-short</git.version.number>
        <nulogix-billing-response-schema>nulogixBillingResponse_1.1.xsd</nulogix-billing-response-schema>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformats-text</artifactId>
            <version>2.10.0.pr1</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>           
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>fluent-hc</artifactId>
            </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.2.11</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>$maven-jaxb2-plugin.version</version>
                <executions>
                    <execution>
                        <id>id1-generate-service-end-point-from-wsdl</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <specVersion>$jaxb2-specVersion</specVersion>
                            <schemaDirectory>$project.basedir/src/main/resources/wsdl</schemaDirectory>
                            <schemaIncludes>
                                <include>*.wsdl</include>
                            </schemaIncludes>
                            <generateDirectory>$project.basedir/target/generated-sources/xjc/billing_endpoint</generateDirectory>
                            <generatePackage>com.nulogix.billing.service</generatePackage>
                        </configuration>
                    </execution>
                    <execution>
                        <id>id2-generate-pojo-from-request-xsd</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <specVersion>$jaxb2-specVersion</specVersion>
                            <schemaDirectory>$project.basedir/src/main/resources/xsd/request</schemaDirectory>
                            <schemaIncludes>
                                <include>StudyDetailsSchema_3.17.6.12.xsd</include>
                            </schemaIncludes>
                            <bindingDirectory>$project.basedir/src/main/resources/xsd/request</bindingDirectory>
                            <bindingIncludes>
                                <include>StudyDetailsSchema_3.17.6.12.xjb</include>
                            </bindingIncludes>
                            <generateDirectory>$project.basedir/target/generated-sources/xjc/request_model</generateDirectory>
                            <generatePackage>com.nulogix.billing.model.request</generatePackage>                            
                        </configuration>                
                    </execution>
                    <execution>
                        <id>id3-generate-pojo-from-response-xsd</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <specVersion>$jaxb2-specVersion</specVersion>
                            <schemaDirectory>$project.basedir/src/main/resources/xsd/response</schemaDirectory>
                            <schemaIncludes>
                                <include>$nulogix-billing-response-schema</include>
                            </schemaIncludes>
                            <generateDirectory>$project.basedir/target/generated-sources/xjc/response_model</generateDirectory>                           
                            <generatePackage>com.nulogix.billing.model.response</generatePackage>                   
                        </configuration>                
                    </execution>
                </executions>
            </plugin>
                  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId> <!-- surefire plugin version managed by Spring Boot -->
   <configuration>
      <skipTests>true</skipTests>
   </configuration>
   <executions>
     <execution>
      <id>unit-tests</id>
      <phase>test</phase>
       <goals>
         <goal>test</goal>
       </goals>
       <configuration>
         <skipTests>false</skipTests>
         <includes>
           <include>**/*Test.java</include>
         </includes>
       </configuration>
     </execution>
     <execution>
       <id>integration-tests</id>
       <phase>integration-test</phase>
       <goals>
        <goal>test</goal>
       </goals>
       <configuration>
         <skipTests>false</skipTests>
         <includes>
           <include>**/*IT.*</include>
        </includes>
       </configuration>
     </execution>
   </executions>
 </plugin>



            <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <dateFormat>yyyyMMdd-HHmmss</dateFormat><!--  human-readable part of the version id -->
                    <dotGitDirectory>$project.basedir/.git</dotGitDirectory>
                    <generateGitPropertiesFile>true</generateGitPropertiesFile><!-- somehow necessary. otherwise the variables are not available in the pom -->
                </configuration>
            </plugin>
            <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-jar-plugin</artifactId>
                  <configuration>
                    <archive>
                      <manifestEntries>
                        <Implementation-Version>$nulogix-release-number-$git.version.number</Implementation-Version>
                        <Git-Version-Number>$git.version.number</Git-Version-Number>
                      </manifestEntries>
                    </archive>
                  </configuration>
            </plugin>

        </plugins>
        <resources>
            <resource>
                <directory>resources</directory>
                <targetPath>$project.build.outputDirectory</targetPath>
                <includes>
                    <include>application.properties</include>
                </includes>
            </resource>
        </resources>
    </build>    
<modules>
   <module>mock_server</module>
</modules>
</project>

这是我的模拟服务器 pom:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.nulogix</groupId>
    <artifactId>billing_mediator</artifactId>
    <version>$nulogix-release-number-$git.version.number</version>
  </parent>
  <groupId>com.nulogix</groupId>
  <artifactId>mock_server</artifactId>
  <version>$nulogix-release-number-$git.version.number</version>
  <name>mock_server</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven-jaxb2-plugin.version>0.14.0</maven-jaxb2-plugin.version>
        <jaxb2-specVersion>2.2</jaxb2-specVersion>
        <git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
        <nulogix-release-number>0.9.6_M2</nulogix-release-number>
        <git.version.number>$git.commit.time.$git.commit.id.describe-short</git.version.number>
        <nulogix-billing-response-schema>nulogixBillingResponse_1.1.xsd</nulogix-billing-response-schema>
         </properties>

  <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformats-text</artifactId>
            <version>2.10.0.pr1</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>           
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>fluent-hc</artifactId>
            </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.2.11</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>$maven-jaxb2-plugin.version</version>
                <executions>
                    <execution>
                        <id>id1-generate-service-end-point-from-wsdl</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <specVersion>$jaxb2-specVersion</specVersion>
                            <schemaDirectory>$project.basedir/src/main/resources/wsdl</schemaDirectory>
                            <schemaIncludes>
                                <include>*.wsdl</include>
                            </schemaIncludes>
                            <generateDirectory>$project.basedir/target/generated-sources/xjc/billing_endpoint</generateDirectory>
                            <generatePackage>com.nulogix.billing.service</generatePackage>
                        </configuration>
                    </execution>
                    <execution>
                        <id>id2-generate-pojo-from-request-xsd</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <specVersion>$jaxb2-specVersion</specVersion>
                            <schemaDirectory>$project.basedir/src/main/resources/xsd/request</schemaDirectory>
                            <schemaIncludes>
                                <include>StudyDetailsSchema_3.17.6.12.xsd</include>
                            </schemaIncludes>
                            <bindingDirectory>$project.basedir/src/main/resources/xsd/request</bindingDirectory>
                            <bindingIncludes>
                                <include>StudyDetailsSchema_3.17.6.12.xjb</include>
                            </bindingIncludes>
                            <generateDirectory>$project.basedir/target/generated-sources/xjc/request_model</generateDirectory>
                            <generatePackage>com.nulogix.billing.model.request</generatePackage>                            
                        </configuration>                
                    </execution>
                    <execution>
                        <id>id3-generate-pojo-from-response-xsd</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <specVersion>$jaxb2-specVersion</specVersion>
                            <schemaDirectory>$project.basedir/src/main/resources/xsd/response</schemaDirectory>
                            <schemaIncludes>
                                <include>$nulogix-billing-response-schema</include>
                            </schemaIncludes>
                            <generateDirectory>$project.basedir/target/generated-sources/xjc/response_model</generateDirectory>                           
                            <generatePackage>com.nulogix.billing.model.response</generatePackage>                   
                        </configuration>                
                    </execution>
                </executions>
            </plugin>
                  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId> <!-- surefire plugin version managed by Spring Boot -->
   <configuration>
      <skipTests>true</skipTests>
   </configuration>
   <executions>
     <execution>
      <id>unit-tests</id>
      <phase>test</phase>
       <goals>
         <goal>test</goal>
       </goals>
       <configuration>
         <skipTests>false</skipTests>
         <includes>
           <include>**/*Test.java</include>
         </includes>
       </configuration>
     </execution>
     <execution>
       <id>integration-tests</id>
       <phase>integration-test</phase>
       <goals>
        <goal>test</goal>
       </goals>
       <configuration>
         <skipTests>false</skipTests>
         <includes>
           <include>**/*IT.*</include>
        </includes>
       </configuration>
     </execution>
   </executions>
 </plugin>



            <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <dateFormat>yyyyMMdd-HHmmss</dateFormat><!--  human-readable part of the version id -->
                    <dotGitDirectory>$project.basedir/.git</dotGitDirectory>
                    <generateGitPropertiesFile>true</generateGitPropertiesFile><!-- somehow necessary. otherwise the variables are not available in the pom -->
                </configuration>
            </plugin>
            <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-jar-plugin</artifactId>
                  <configuration>
                    <archive>
                      <manifestEntries>
                        <Implementation-Version>$nulogix-release-number-$git.version.number</Implementation-Version>
                        <Git-Version-Number>$git.version.number</Git-Version-Number>
                      </manifestEntries>
                    </archive>
                  </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我的模拟服务器应用会运行,但我的主应用不会(中介)

【问题讨论】:

你的问题归根结底是“版本”不应该包含表达式。 更具体地说,git.version.number 由插件动态设置,但在调用插件之前是必需的。 @Michael 我应该放什么?我尝试输入 0.9.6_M2 作为版本号,但它说找不到那个工件 【参考方案1】:

如果您想在项目的版本号中使用属性,则需要对 ci 友好的属性 (https://maven.apache.org/maven-ci-friendly.html)。其他属性只能在依赖中使用,不能在自己的版本号中使用。

【讨论】:

每当我尝试设置版本号时,它都会说找不到本地工件 jar。是不是因为main被声明为POM,找不到jar? 对不起,我不明白你的问题。你纠正&lt;version&gt;$nulogix-release-number-$git.version.number&lt;/version&gt;了吗? 我仍然对如何修复它感到困惑,我不确定该放什么。 只有三个属性可以成为版本号的一部分。这些是$revision$sha1$changelist 它仍然说找不到 $revision 的 artifact.jar

以上是关于Maven 使用 mvn test 版本号时无法解析依赖关系的主要内容,如果未能解决你的问题,请参考以下文章

遇到问题--java--mac版本的idea在控制台中使用mvn命令not found

遇到问题--java--mac版本的idea在控制台中使用mvn命令not found

遇到问题--java--mac版本的idea在控制台中使用mvn命令not found

如何使用 maven mvn test 命令行运行动态 testng.xml?

mvn test 指定suit

mvn test 指定suit