Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - throw exception if project still has SNAPSHOT dependencies

I want to fail the gradle build if the current project still has snapshot dependencies.

My code so far only looks for java dependencies, missing the .NET ones so it only works for java projects. I want to make it work for all projects.

def addSnapshotCheckingTask(Project project) {
    project.tasks.withType(JavaCompile) { compileJava ->
        project.tasks.create(compileJava.name + 'SnapshotChecking', {
            onlyIf {
                project.ext.isRelease || project.ext.commitVersion != null
            }
            compileJava.dependsOn it
            doLast {
                def snapshots = compileJava.classpath
                        .filter { project.ext.isRelease || !(it.path ==~ /(?i)${project.rootProject.projectDir.toString().replace('\\', '\\\\')}.*build.libs.*/) }
                        .filter { it.path =~ /(?i)-SNAPSHOT/  }
                        .collect { it.name }
                        .unique()
                if (!snapshots.isEmpty()) {
                    throw new GradleException("Please get rid of snapshots for following dependencies before releasing $snapshots")
                }
            }
        })
    }
}

I need some help in generifying this snippet to be applicable to all types of dependencies(not just java)

Thanks!

L.E. Could something like this work? https://discuss.gradle.org/t/how-can-i-check-for-snapshot-dependencies-and-throw-an-exception-if-some-where-found/4064

like image 992
George Cimpoies Avatar asked Sep 11 '18 12:09

George Cimpoies


People also ask

Can't resolve all dependencies for configuration Gradle?

This is because of slow internet connection or you haven't configure proxy settings correctly. Gradle needs to download some dependencies , if it cant access the repository it fires this error. All you have to do is check your internet connection and make sure gradle can access the maven repository. Save this answer.

How does Gradle resolve transitive dependencies?

Transitive dependencyBy default, Gradle resolves transitive dependencies automatically. The version selection for transitive dependencies can be influenced by declaring dependency constraints.

What is rootDir in Gradle?

rootDir. The root directory of this project. The root directory is the project directory of the root project. rootProject.

What is runtimeOnly in Gradle?

implementation configuration is used for compile and runtime classpath but it is only exposed to the consumers for their runtime classpath. runtimeOnly configuration is only used for the runtime classpath and it is also exposed to the consumers for their runtime classpath.


2 Answers

So I got it working by tweaking a bit the response of @lance-java, it looks something like:

    Task snapshotCheckingTask = project.tasks.create('snapshotCheckingTask', {
        doLast {
            def snapshots = new ArrayList()
            def projectConfigurations = project.configurations.findAll { true }

            projectConfigurations.each {
                if (it.isCanBeResolved()) {
                    it.resolvedConfiguration.resolvedArtifacts.each {
                        if (it.moduleVersion.id.version.endsWith('-SNAPSHOT')) {
                            snapshots.add(it)
                        }
                    }
                }
            }
            if (!snapshots.isEmpty()) {
                throw new GradleException("Please get rid of snapshots for following dependencies before releasing $snapshots")
            } else {
                throw new GradleException("Hah, no snapshots!")
            }
        }
    })
    project.tasks.release.dependsOn snapshotCheckingTask

cc @Eugene

P.S. However, this does not take into account .net dependencies

like image 191
George Cimpoies Avatar answered Sep 20 '22 16:09

George Cimpoies


Something like

Collection<ResolvedArtifact> snapshotArtifacts = project.configurations*.resolvedConfiguration.resolvedArtifacts.filter { it.moduleVersion.id.version.endsWith('-SNAPSHOT') }
if (!snapshotArtifacts.empty) {
   // throw exception
}

See https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/Configuration.html#getResolvedConfiguration-- https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/ResolvedConfiguration.html#getResolvedArtifacts--

like image 44
lance-java Avatar answered Sep 20 '22 16:09

lance-java