在本地发布Gradle快照

10

这是我的dummy项目的build.gradle文件:

apply plugin: 'groovy'

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.3'
    compile 'org.slf4j:jcl-over-slf4j:1.7.7'

    testCompile 'junit:junit:4.11'
    testCompile 'org.mockito:mockito-all:1.10.8'
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

artifacts {
    archives sourcesJar
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

当我运行gradle clean build -Pversion=1.2.3时,它将打包我的所有代码并为我创建build/libs/dummy-1.2.3.jar

我想知道最少量的Gradle代码,以便我可以将"dummy" JARs发布到本地Maven缓存(mavenLocal())。此外,这与版本控制有何关系?我可以为虚拟JAR指定具体版本,但在本地发布时,使用SNAPSHOT版本更为合理(至少对我来说)。我可以只运行gradle clean build -Pversion=0.1.SNAPSHOT,但我的担忧是其他本地项目是否会获取最新的SNAPSHOT版本。

所以:

  1. 发布dummy的最少代码是什么?
  2. 在本地发布时,是否有任何方法指定其他项目始终获取最新的SNAPSHOT版本?
  3. 其他本地项目需要使用什么来获取这个SNAPSHOT?例如compile ':dummy:LATEST'

1
你所提出的问题是否已经得到了回答?如果是,请接受答案。 - Opal
2个回答

9

这里,我为您准备了一个示例项目,这是关于build.gradle配置的最基本要求。

  1. You need to add apply plugin: 'maven' and set group = 'somegroup'. maven plugin gives install task and group is required to install artifact in maven repository.

    Then run gradle clean install. If no version passed, it will evaluate to unspecified, if no artifactId configured it will evaluate to project.name. Here You can find how to configure other maven properties.

    Installing snapshot for local development is definitely a good idea.

  2. If You'd like other project to always pick the latest version of SNAPSHOT You need to add the following piece of code to build.gradle scripts. It guarantees resolving to the latest version.

    configurations.all {
       resolutionStrategy {
         cacheChangingModulesFor 0, 'seconds'
       }
    }
    
  3. First of all You need to add snapshot repo to repositories block (just a sample) - with local maven repo, this step is not needed:

    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots'
    }
    

    Dependency should be specified as:

    group:artifact:0.+
    

如有任何问题,请随时提出。


2

1) 您只需要执行一个

gradle install

2) 指定您喜欢的任何版本- 一个 SNAPSHOT 版本对于活跃的开发是有意义的。一旦您认为您的库不太可能改变,您应该绝对选择非快照版本。

3) 您必须指定对本地存储库中版本的依赖项,就像对第三方库一样。


7
需要哪个插件才能执行这个操作?当我尝试时会得到以下信息:在根项目 'kotti' 中找不到任务 'install'。 - Adeynack

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接