Gradle unable to resolve maven profile dependency

2019-06-11 08:28发布

问题:

I have a maven project A that has profiles in it as:

<properties>
<bigquery.version>0.0.1</bigquery.version>
</properties>
.
.
<profiles>
<profile>
  <id>dev</id>
  <activation></activation>
  <properties>
    <build.version>${bigquery.version}</build.version>
  </properties>
</profile>
<profile>
  <id>screwdriver-v3</id>
  <activation>
    <activeByDefault>true</activeByDefault>
    <property>
      <name>screwdriver3</name>
    </property>
  </activation>
  <properties>
    <buildtype>release</buildtype>
    <build.version>${bigquery.version}.${maven.build.timestamp}</build.version>
  </properties>
</profile>

I have created a version of A at : 0.0.1.20180424-0042

I have another gradle project B that I want to add A as a dependency as:

compile group:'com.bq', name:'bigquery', version:'0.0.1.20180424-0042'

When I build gradle with ./gradlew clean build, it is complaining as:

Could not resolve all files for configuration ':compileClasspath'.
> Could not resolve com.bq:bigquery:0.0.1.20180424-0042.
Required by:
  project :
> Could not resolve com.bq:bigquery:0.0.1.20180424-0042.
  > inconsistent module metadata found. Descriptor: com.bq:bigquery:0.0.1.${maven.build.timestamp} Errors: bad version: expected='0.0.1.20180424-0042' found='0.0.1.${maven.build.timestamp}'

How do I go about fixing the dependency?

回答1:

when maven creating versions then it is adding timestamp at the end, for example in nexus I was checking for you there is a some artifact with version 0.0.3-SNAPSHOT but jar is 0.0.3-20161026.145419-3 , so most problably what you need to change is this: compile group:'com.bq', name:'bigquery', version:'0.0.1.20180424-0042' -> compile group:'com.bq', name:'bigquery', version:'0.0.1'

Note: in your pom.xml file you also have 0.0.1



回答2:

Using maven profiles like this is considered an anti-pattern, further reading here

Gradle does not have full support for maven profile activation. Read the blog post here to see that the two supported profile activation triggers are

  • Profiles that are active by default (available with Gradle 1.12 and higher)
  • Profiles that are active on the absence of a system property (available with Gradle 2.0 and higher)

As a workaround, you could do the following:

  1. Use a local folder as a hacked maven repository
  2. Order the local folder repository first so it takes precedence over the remote repository
  3. Copy / paste / tweak the pom in the local file repo so that it hard codes the version
  4. Do not copy the artifact(s) (eg jar) to the local folder repository

Then gradle should take the pom from the local repo and the jars etc from the remote repository

Eg:

repositories {
    maven {
        url = file('local-repo')
    }
    mavenCentral()
}

You would then put the "tweaked" pom at

$projectDir/local-repo/$group.replace('.','/')/$artifact/$version/$artifactId-$version.pom


标签: maven gradle