Maven依赖包判断

2022/09/01 Maven

Maven依赖包判断

生产环境不允许使用快照包,但人为规定终究不如脚本进行检测,所以在打war包,检测是否引用了快照包,如果引用了宣布打包失败

Jenkins打包后的pipeline

在pipeline的script指令里,添加如下groovy脚本,其中job_name是项目的名字,一般打完包后,会在这里的lib有引用的jar包文件

    exitValue = sh(script: "ls $workspace/${job_name}/target/${job_name}/WEB-INF/lib |grep 'SNAPSHOT.jar'", returnStatus: true)
    echo "return exitValue :${exitValue}"
    if(exitValue == 0){
        error("ERROR:此版本包含SNAPSHOT软件包,不允许使用SNAPSHOT")
    }

使用maven-enforcer-plugin检查。

可以使用maven-enforcer-plugin仔细检查是否仍存在任何SNAPSHOT依赖关系。

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.4.1</version>
        <executions>
          <execution>
            <id>enforce-no-snapshots</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireReleaseDeps>
                  <message>No Snapshots Allowed!</message>
                </requireReleaseDeps>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

实例如下

[INFO] 
[INFO] ---------------------------< com.demo:demo >----------------------------
[INFO] Building demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-no-snapshots) @ demo ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireReleaseDeps failed with message:
No Snapshots Allowed!
Found Banned Dependency: com.qjdchina:cif-service:jar:2.0.9-SNAPSHOT
Use 'mvn dependency:tree' to locate the source of the banned dependencies.

请注意fail元素设置为true,在这种情况下,如果找到任何SNAPSHOT依赖项,构建将失败。

您可以将此类配置放在maven profile中并在需要时激活它(因此每当必须执行此检查时)。

Search

    微信好友

    博士的沙漏

    Table of Contents