gpt4 book ai didi

java - 将我的Kotlin Maven发布代码转换为Java Gradle插件

转载 作者:行者123 更新时间:2023-12-03 05:45:42 27 4
gpt4 key购买 nike

这是我一直复制粘贴到每个gradle.build.kts的kotlin代码

System.getenv("JRS_S3_URI")?.let {
val sourcesJar by tasks.creating(Jar::class) {
classifier = "sources"
from(java.sourceSets["main"].allSource)
}

publishing {
repositories {
maven {
url = uri(it)
credentials(AwsCredentials::class.java) {
accessKey = System.getenv("JRS_ACCESSKEYID")
secretKey = System.getenv("JRS_SECRETACCESSKEY")
}
}
}
(publications) {
"mavenJava"(MavenPublication::class) {
from(components["java"])
artifact(sourcesJar)
}
}
}
}

到目前为止,这是我提出的明智的插件
public class Maven extends SourceSetConfiguredPlugin {

public Maven( XenoExtension extension ) {
super( extension );
}


@Override
public void apply( Project project ) {
if ( System.getenv( "JRS_S3_URI" ) != null ) {
project.getPluginManager().apply( MavenPublishPlugin.class );
TaskContainer tasks = project.getTasks();
Jar sourceSet = tasks.create( "sources", Jar.class, task -> {
getSourceSets().stream()
.map( SourceSet::getAllSource )
.map( sds -> project.copySpec().from( sds ) )
.forEach( copySpec -> task.include( copySpec.getIncludes() ) );
} );

tasks.withType( PublishToMavenRepository.class, task -> {
task.getRepository().setUrl( URI.create( System.getenv( "JRS_S3_URI" ) ) );
task.getRepository().credentials( AwsCredentials.class, creds -> {
creds.setAccessKey( System.getenv( "JRS_ACCESSKEYID" ) );
creds.setSecretKey( System.getenv( "JRS_SECRETACCESSKEY" ) );
} );
MavenPublication publication = task.getPublication();
publication.artifact( project.getComponents() );
publication.artifact( sourceSet );
} );

}
}
}

它可以编译,但显然不会在运行时将源和二进制jar推送到我的存储库中。如何修正我的插件,使其与Kotlin代码相同?

最佳答案

尽管事后看来似乎很明显,但这是执行相同操作的代码。

@Override
public void apply( Project project ) {
if ( System.getenv( "JRS_S3_URI" ) != null ) {
log.info( "publishing to: {}", System.getenv( "JRS_S3_URI" ) );
project.getPluginManager().apply( MavenPublishPlugin.class );
TaskContainer tasks = project.getTasks();
project.getExtensions().configure( PublishingExtension.class, ext -> {
ext.repositories( repos -> {
repos.maven( repo -> {
repo.setUrl( URI.create( System.getenv( "JRS_S3_URI" ) ) );
repo.credentials( AwsCredentials.class, creds -> {
creds.setAccessKey( System.getenv( "JRS_ACCESSKEYID" ) );
creds.setSecretKey( System.getenv( "JRS_SECRETACCESSKEY" ) );
} );
} );
} );
ext.publications( pubs -> {
Jar sourceJar = tasks.create( "sources", Jar.class, task -> {
task.setClassifier( task.getName() );
getSourceSets().stream()
.map( SourceSet::getAllSource )
.forEach( task::from );
} );

pubs.create( "mavenJava", MavenPublication.class, pub -> {
pub.from( project.getComponents().getAt( "java" ) );
pub.artifact( sourceJar );
} );
} );
} );
}
else {
log.info( "no repository to publish to defined" );
}
}

关于java - 将我的Kotlin Maven发布代码转换为Java Gradle插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50244489/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com