Two classes on the buildscript classpath with the same name

I am working on a project whose build.gradle includes the following line:

def availablePortFinder = AvailablePortFinder.createPrivate()
props.setProperty(‘mcast-port’, Integer.toString(availablePortFinder.nextAvailable))

AvailablePortFinder was deprecated and removed in Gradle 3.0, so I am trying to replace org.gradle.util.AvailablePortFinder with org.apache.mina.util.AvailablePortFinder. I have added the following configuration:

buildscript {
repositories {
mavenCentral()
maven { url “https://plugins.gradle.org/m2/” }
}

dependencies {
classpath group: ‘org.apache.mina’, name: ‘mina-core’, version: ‘2.0.14’
}
}

However, I don’t know how to qualify which AvailablePortFinder I want to use. I have tried fully qualifying the classname:

props.setProperty(‘mcast-port’, Integer.toString(org.apache.mina.util.AvailablePortFinder.AvailablePortFinder.getNextAvailable()))

which gave this error

Could not get unknown property ‘org’ for root project ‘XXX’ of type org.gradle.api.Project.

as well as a static import

import static org.apache.mina.util.AvailablePortFinder.AvailablePortFinder.getNextAvailable

props.setProperty(‘mcast-port’, Integer.toString(AvailablePortFinder.getNextAvailable(49152)))

which gave this error

  • What went wrong:
    Could not compile script ‘XXXXX/build.gradle’.

startup failed:
script ‘XXXXX/build.gradle’: 1: unable to resolve class org.apache.mina.util.AvailablePortFinder
@ line 1, column 1.
import org.apache.mina.util.AvailablePortFinder
^

1 error

I think you may have an extra AvailablePortFinder. Is it org.apache.mina.util.AvailablePortFinder.getNextAvailable()?

Thank you for your response. I did have an extra AvailablePortFinder in there, however I still get this error after correcting it:

1: Task failed with an exception.

  • Where:
    Script ‘/gradle/test.gradle’ line: 23
  • What went wrong:
    Execution failed for task ‘:extensions/geode-modules:test’.

Could not get unknown property ‘org’ for root project ‘XXX’ of type org.gradle.api.Project.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

So it turns out that Java doesn’t allow class import aliasing, but Groovy does. This was what I needed:

import org.apache.mina.util.AvailablePortFinder as MinaAvailablePortFinder