2323import org .gradle .api .publish .PublishingExtension ;
2424import org .gradle .api .publish .maven .MavenPom ;
2525import org .gradle .api .publish .maven .MavenPublication ;
26+ import org .gradle .api .publish .maven .tasks .PublishToMavenRepository ;
2627
2728import java .io .File ;
29+ import java .net .URL ;
2830
2931import static org .eclipse .edc .plugins .edcbuild .conventions .ConventionFunctions .requireExtension ;
32+ import static org .gradle .api .artifacts .ArtifactRepositoryContainer .MAVEN_CENTRAL_URL ;
3033
3134/**
3235 * Configures the Maven POM for each project:
@@ -46,9 +49,14 @@ public void apply(Project target) {
4649 var pomExt = requireExtension (project , BuildExtension .class ).getPom ();
4750
4851 pubExt .getPublications ().stream ()
49- .filter (p -> p instanceof MavenPublication )
50- .map (p -> ( MavenPublication ) p )
52+ .filter (MavenPublication . class :: isInstance )
53+ .map (MavenPublication . class :: cast )
5154 .peek (mavenPub -> mavenPub .pom (pom -> setPomInformation (pomExt , target , pom )))
55+ .peek (publication -> {
56+ if (!publication .getVersion ().endsWith ("-SNAPSHOT" )) {
57+ disablePublishIfArtifactAlreadyUploaded (publication , project );
58+ }
59+ })
5260 .forEach (mavenPub -> {
5361 var openapiFiles = target .getLayout ().getBuildDirectory ().getAsFile ().get ().toPath ()
5462 .resolve ("docs" ).resolve ("openapi" ).toFile ()
@@ -68,6 +76,24 @@ public void apply(Project target) {
6876 });
6977 }
7078
79+ private void disablePublishIfArtifactAlreadyUploaded (MavenPublication publication , Project project ) {
80+ project .getTasks ().withType (PublishToMavenRepository .class ).forEach (task -> {
81+ var artifact = publication .getGroupId () + ":" + publication .getArtifactId () + ":" + publication .getVersion ();
82+
83+ try {
84+ var artifactPath = MAVEN_CENTRAL_URL + publication .getGroupId ().replace ('.' , '/' ) +
85+ "/" + publication .getArtifactId () + "/" + publication .getVersion () +
86+ "/" + publication .getArtifactId () + "-" + publication .getVersion () + ".pom" ;
87+
88+ new URL (artifactPath ).openStream ().close ();
89+ project .getLogger ().debug ("Artifact {} already exists - skipping publish" , artifact );
90+ task .setEnabled (false );
91+ } catch (Throwable e ) {
92+ project .getLogger ().debug ("Artifact {} not found on maven central - will publish" , artifact );
93+ }
94+ });
95+ }
96+
7197 private String getFilenameWithoutExtension (File openapiFile ) {
7298 return openapiFile .getName ().substring (0 , openapiFile .getName ().lastIndexOf ("." ));
7399 }
0 commit comments