26

私はmavensettings.xmlを次の場所に持っています:

 /home/u123/.m2/settings.xml

ここで、リモートMavenリポジトリを指定します。

<?xml version="1.0" encoding="UTF-8"?>
<settings>
  <profiles>
    <profile>
      <id>default</id>
      <repositories>
          <repository>
              <id>my.repo</id>
               <url>http://myrepo/ </url>
          </repository>
      </repositories>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>default</activeProfile>
  </activeProfiles>
</settings>

このリポジトリでは、アーティファクトをデプロイしました。実際には、そのgradleプラグインです。

次に、以下のbuild.gradleファイルを使用して、このプラグイン/アーティファクトを使用する必要がある別のプロジェクトをビルドしようとします。

apply plugin: 'java'
apply plugin: 'maven'

buildscript {
    dependencies {
        classpath 'com.test:my-gradle-plugin:1.0.0-SNAPSHOT'
    }
}

しかし、ビルドは失敗します:

...
> Could not find group:com.test, module:my-gradle-plugin, version:1.0.0-SNAPSHOT.
...

上記のsettings.xmlファイルがリモートのMavenリポジトリを指しているのに、Gradleがmy-gradle-pluginを見つけることができません。

代わりに、build.gradleファイルのリポジトリを指定すると、次のように機能します。

buildscript {
    repositories {
        maven {
            url "http://myrepo/"
        }
    }
    dependencies {
        classpath 'com.test:my-gradle-plugin:1.0.0-SNAPSHOT'
    }
}

この投稿に基づく:

http://forums.gradle.org/gradle/topics/have_mavenlocal_check_m2_home_conf_settings_xml

gradleはsettings.xmlファイルを考慮しているようですが、何が問題になっていますか?

4

6 に答える 6

26

これに関連するオープンチケットがあり、うまくいけば実装されます。

http://issues.gradle.org/browse/GRADLE-2365

ただし、回避策として、build.gradleでGroovyスクリプトを使用してこれを実現できます。私の場合、settings.xmlからの認証情報が必要でした。しかし、これはリポジトリ情報を取得するために簡単に適応させることができます。

例:

def getMavenSettingsCredentials = {
    String userHome = System.getProperty( "user.home" );
    File mavenSettings = new File(userHome, ".m2/settings.xml")
    def xmlSlurper = new XmlSlurper()
    def output = xmlSlurper.parse(mavenSettings)
    return output."servers"."server"
}

def getCredentials = {
    def entries = getMavenSettingsCredentials()
    for (entry in entries) {
        if ( entry."id".text() == "my-server" ) {
            return [username: entry.username.text(), password: entry.password.text()]
    }
  }
}
uploadArchives {
def creds = getCredentials()
repositories.mavenDeployer {
    configuration = configurations.deployerJars
    repository(url: "http://my-release-repository/releases/") {
        authentication(userName: creds["username"], password: creds["password"])
    }
    snapshotRepository(url: "http://my-snapshot-repository/snapshots/") {
        authentication(userName: creds["username"], password: creds["password"])
    }


  }
}
于 2013-03-14T07:17:32.290 に答える
9

gradle-maven-settings-pluginは私のために機能します(少なくとも私のWindows環境では)

追加する必要があります

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
}

plugins {
    id 'net.linguica.maven-settings' version '0.5'
}

に、次のbuild.gradleようにリポジトリを追加できます。

repositories {
    maven {
        name = 'myRepo' // should match <id>myRepo</id> of appropriate <server> in Maven's settings.xml
        url = 'https://intranet.foo.org/repo'
    }
}

これはmyRepo、Mavenのクレデンシャルを使用してhttps://intranet.foo.org/repoリポジトリsettings.xmlにアクセスします

于 2018-05-26T19:26:27.370 に答える
7

Gradleビルドスクリプトですべてのリポジトリを宣言する必要があります。settings.xmlを解決する場合など、ローカルのMavenリポジトリの場所を見つけるためにのみ使用されますrepositories { mavenLocal() }

于 2012-09-21T00:20:48.270 に答える
4

maven-publish、maven-publish-authプラグインを使用して、設定を手動で解析せずにこれを実現しました

GradleがMavenのSettings.xmlからリポジトリ設定を使用してアーティファクトを公開する方法

それがお役に立てば幸いです。

ピーター

于 2014-02-10T18:39:22.967 に答える
3

参照:https ://github.com/ci-and-cd/maven-settings-decoder

build.gradleまたは環境変数でnexusパスワードを公開しないように、gradleビルドスクリプトで使用します。

buildscript {
  repositories {
    ...
    mavenCentral()
  }
  dependencies {
    ...
    classpath 'cn.home1.tools:maven-settings-decoder:1.0.5.OSS'
  }
}
...
ext.mavenSettings = new cn.home1.tools.maven.SettingsDecoder();
ext.nexusSnapshotsUser = mavenSettings.getText("//server[id='${nexus}-snapshots']/username/text()")
ext.nexusSnapshotsPass = mavenSettings.getText("//server[id='${nexus}-snapshots']/password/text()")
println "${nexus}-snapshots username: " + mavenSettings.getText("//server[id='${nexus}-snapshots']/username/text()")
println "${nexus}-snapshots password: " + mavenSettings.getText("//server[id='${nexus}-snapshots']/password/text()")
...
于 2016-12-28T07:40:37.587 に答える
1

build.gradleファイルのリポジトリセクションでmavenLocal()を使用してください。これにより、ホームディレクトリの〜/ .m2/settings.xmlファイルが読み取られます。

repositories {
    mavenCentral()
    mavenLocal()
}
于 2018-11-08T22:12:37.123 に答える