Skip to content

Commit 679592e

Browse files
committed
fix issue #7030 - enable sonatype snapshot deployments
Signed-off-by: Peter Kirschner <peter@klib.io>
1 parent 285c999 commit 679592e

File tree

8 files changed

+71
-45
lines changed

8 files changed

+71
-45
lines changed

biz.aQute.repository/src/aQute/bnd/repository/maven/provider/MavenBndRepository.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public class MavenBndRepository extends BaseRepository implements RepositoryPlug
102102
Refreshable, Actionable, ToDependencyPom, ReleaseBracketingPlugin {
103103

104104
public static final String SONATYPE_RELEASE_DIR = "cnf/cache/sonatype-release";
105+
public static final String SONATYPE_SNAPSHOT_DIR = "cnf/cache/sonatype-snapshot";
105106
public static final String SONATYPE_DEPLOYMENTID_FILE = "deploymendID.txt";
106107

107108
final static Pattern PREPROCESS_P = Pattern
@@ -264,9 +265,9 @@ public PutResult put(InputStream stream, PutOptions options) throws Exception {
264265
}
265266

266267
doExtra(options, instructions, pom, releaser);
267-
}
268-
if (configuration.noupdateOnRelease() == false) {
269-
index.add(binaryArchive);
268+
if (configuration.noupdateOnRelease() == false) {
269+
index.add(binaryArchive);
270+
}
270271
}
271272
}
272273
return result;
@@ -655,45 +656,57 @@ synchronized boolean init() {
655656
try {
656657
List<MavenBackingRepository> release = new ArrayList<MavenBackingRepository>();
657658
MavenBackingRepository staging = null;
659+
List<MavenBackingRepository> snapshot = new ArrayList<MavenBackingRepository>();
658660

659661
String releaseUrl = configuration.releaseUrl();
660662
SonatypeMode sonatypeMode = configuration.sonatypeMode(SonatypeMode.NONE.name());
661663

662664
String stagingUrl = configuration.stagingUrl();
665+
String snapshotUrl = configuration.snapshotUrl();
663666

664-
String sonatypeUrl = null;
667+
String sonatypeReleaseUrl = null;
668+
String sonatypeSnapshotUrl = null;
665669
switch (sonatypeMode) {
666670
case MANUAL, AUTOPUBLISH -> {
667671
logger.info("deployment via Sonatype Central Portal configured in {} mode", sonatypeMode);
668672
File releaseDir = registry.getPlugin(Workspace.class)
669673
.getFile(SONATYPE_RELEASE_DIR);
674+
File snapshotDir = registry.getPlugin(Workspace.class)
675+
.getFile(SONATYPE_SNAPSHOT_DIR);
670676
if (stagingUrl == null) {
671677
logger.debug("deployment via relase url to Sonatype Portal configured");
672678
List<MavenBackingRepository> releaseLocal = MavenBackingRepository.create(releaseDir.toURI()
673679
.toString(), reporter, localRepo, client);
674680
release.addAll(releaseLocal);
675-
sonatypeUrl = releaseUrl;
681+
sonatypeReleaseUrl = releaseUrl;
676682
} else {
677683
logger.debug("deployment via staging url to Sonatype Portal configured");
678684
release = MavenBackingRepository.create(releaseUrl, reporter, localRepo, client);
679685
staging = MavenBackingRepository.getBackingRepository(releaseDir.toURI()
680686
.toString(), reporter, localRepo, client);
681-
sonatypeUrl = stagingUrl;
687+
sonatypeReleaseUrl = stagingUrl;
688+
}
689+
if (snapshotUrl != null) {
690+
logger.debug("deployment via snapshot url to Sonatype Portal configured");
691+
List<MavenBackingRepository> snapshotLocal = MavenBackingRepository.create(snapshotDir.toURI()
692+
.toString(), reporter, localRepo, client);
693+
snapshot.addAll(snapshotLocal);
694+
sonatypeSnapshotUrl = snapshotUrl;
682695
}
683696
}
684697
case NONE -> {
685698
if (stagingUrl == null) {
686699
release = MavenBackingRepository.create(releaseUrl, reporter, localRepo, client);
687700
} else {
688701
release = MavenBackingRepository.create(releaseUrl, reporter, localRepo, client);
689-
staging = MavenBackingRepository.getBackingRepository(configuration.stagingUrl(), reporter,
690-
localRepo, client);
702+
staging = MavenBackingRepository.getBackingRepository(stagingUrl, reporter, localRepo, client);
703+
}
704+
if (snapshotUrl != null) {
705+
snapshot = MavenBackingRepository.create(snapshotUrl, reporter, localRepo, client);
691706
}
692707
}
693708
}
694709

695-
List<MavenBackingRepository> snapshot = MavenBackingRepository.create(configuration.snapshotUrl(), reporter,
696-
localRepo, client);
697710

698711
for (MavenBackingRepository mbr : release) {
699712
if (mbr.isRemote()) {
@@ -713,8 +726,9 @@ synchronized boolean init() {
713726
.executor(), reporter);
714727
MavenRepository storageMvn = (MavenRepository) storage;
715728
storageMvn.setSonatypeMode(sonatypeMode);
716-
if (sonatypeUrl != null) {
717-
storageMvn.setSonatypePublisherUrl(sonatypeUrl);
729+
if (sonatypeReleaseUrl != null) {
730+
storageMvn.setSonatypePublisherUrl(sonatypeReleaseUrl);
731+
storageMvn.setSonatypePublishSnapshotUrl(sonatypeSnapshotUrl);
718732
}
719733

720734
File indexFile = getIndexFile();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@Version("2.2.0")
1+
@Version("2.3.0")
22
package aQute.bnd.repository.maven.provider;
33

44
import org.osgi.annotation.versioning.Version;

biz.aQute.repository/src/aQute/maven/provider/MavenRepository.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public class MavenRepository implements IMavenRepo, Closeable {
4848
private final Map<Revision, Promise<POM>> poms = new WeakHashMap<>();
4949
private final Reporter reporter;
5050
private SonatypeMode sonatypeMode = SonatypeMode.NONE;
51-
private String sonatypeUrl = null;
51+
private String sonatypeReleaseUrl = null;
52+
private String sonatypeSnapshotUrl = null;
5253

5354
public MavenRepository(File base, String id, List<MavenBackingRepository> release,
5455
List<MavenBackingRepository> snapshot, Executor executor, Reporter reporter) throws Exception {
@@ -418,11 +419,20 @@ public SonatypeMode getSonatypeMode() {
418419
return sonatypeMode;
419420
}
420421

421-
public void setSonatypePublisherUrl(String sonatypeUrl) {
422-
this.sonatypeUrl = sonatypeUrl;
422+
public void setSonatypePublisherUrl(String sonatypeReleaseUrl) {
423+
this.sonatypeReleaseUrl = sonatypeReleaseUrl;
423424
}
424425

425426
public String getSonatypePublisherUrl() {
426-
return sonatypeUrl;
427+
return sonatypeReleaseUrl;
427428
}
429+
430+
public void setSonatypePublishSnapshotUrl(String sonatypeSnapshotUrl) {
431+
this.sonatypeSnapshotUrl = sonatypeSnapshotUrl;
432+
}
433+
434+
public String getSonatypePublishSnapshotUrl() {
435+
return sonatypeSnapshotUrl;
436+
}
437+
428438
}

biz.aQute.repository/src/aQute/maven/provider/Releaser.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void close() throws IOException {
9292
if (Boolean.parseBoolean(isStartSonatypePublish)) {
9393
switch (home.getSonatypeMode()) {
9494
case NONE -> logger.info("Sonatype mode is 'none', nothing to do");
95-
case MANUAL, AUTOPUBLISH -> prepareSonatypeUpload();
95+
case MANUAL, AUTOPUBLISH -> prepareSonatypeUpload(localMetadata.version.isSnapshot());
9696
}
9797
}
9898
home.clear(revision);
@@ -104,41 +104,52 @@ public void close() throws IOException {
104104
}
105105
}
106106

107-
private void prepareSonatypeUpload() throws IOException, Exception {
108-
MavenBackingRepository mbr = home.getStagingRepository();
109-
publisherUrl = normalize(home.getSonatypePublisherUrl());
110-
if (mbr == null) {
111-
List<MavenBackingRepository> releaseRepositories = home.getReleaseRepositories();
112-
if (!releaseRepositories.isEmpty()) {
113-
mbr = releaseRepositories.get(0);
107+
private void prepareSonatypeUpload(boolean isSnapshot) throws IOException, Exception {
108+
MavenBackingRepository mbr = null;
109+
110+
List<MavenBackingRepository> releaseRepositories = new ArrayList<MavenBackingRepository>();
111+
if (isSnapshot) {
112+
publisherUrl = normalize(home.getSonatypePublishSnapshotUrl());
113+
releaseRepositories = home.getSnapshotRepositories();
114+
} else {
115+
publisherUrl = normalize(home.getSonatypePublisherUrl());
116+
mbr = home.getStagingRepository();
117+
if (mbr != null) {
118+
releaseRepositories.add(mbr);
114119
} else {
115-
throw new IllegalStateException("No release repository configured for Sonatype upload");
120+
releaseRepositories = home.getReleaseRepositories();
116121
}
117122
}
123+
if (releaseRepositories.isEmpty()) {
124+
throw new IllegalStateException("No release/snapshot repository configured for Sonatype upload");
125+
} else {
126+
mbr = releaseRepositories.get(0);
127+
}
128+
118129
logger.info("Creating and uploading deployment bundles for Sonatype Central Portal");
119130
MavenFileRepository mfr = (MavenFileRepository) mbr;
120131
client = mfr.getClient();
121-
132+
122133
// Create archives for each groupId
123134
List<MavenFileRepository.GroupIdArchive> archives = mfr.createZipArchive();
124135
if (archives.isEmpty()) {
125136
throw new IllegalStateException("No groupIds found in staging repository");
126137
}
127-
138+
128139
logger.info("Found {} groupId(s) to upload", archives.size());
129-
140+
130141
// Upload each archive separately
131142
for (MavenFileRepository.GroupIdArchive archive : archives) {
132143
logger.info("Processing groupId: {}", archive.groupId);
133144
uploadToPortal(archive.archiveFile);
134-
145+
135146
// Store deployment ID file with groupId in filename
136147
String sanitizedGroupId = archive.getSanitizedGroupId();
137148
File deploymentIdFile = Files.createTempFile(sanitizedGroupId + "_deploymentid", ".txt")
138149
.toFile();
139150
Files.writeString(deploymentIdFile.toPath(), deploymentId, StandardOpenOption.CREATE);
140151
deploymentIdFile.deleteOnExit();
141-
152+
142153
String deploymentIdPath = sanitizedGroupId + "_" + MavenBndRepository.SONATYPE_DEPLOYMENTID_FILE;
143154
mbr.store(deploymentIdFile, deploymentIdPath);
144155
logger.info("Completed upload for groupId: {} with deployment ID: {}", archive.groupId, deploymentId);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version 2.7
1+
version 2.8

biz.aQute.repository/test/aQute/bnd/repository/maven/provider/SonatypeDeploymentTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import org.junit.jupiter.api.BeforeAll;
2121
import org.junit.jupiter.api.BeforeEach;
22-
import org.junit.jupiter.api.Disabled;
2322
import org.junit.jupiter.api.Test;
2423

2524
import aQute.bnd.build.Project;
@@ -41,9 +40,8 @@ public class SonatypeDeploymentTest {
4140

4241
File sonatypeRepoFile = new File(wsDir, "cnf/ext/sonatype_release.bnd");
4342
File releasedVersionFile = new File(wsDir, "cnf/ext/gav_30_sonatype.mvn");
44-
File deploymentIDFile = new File(wsDir,
45-
MavenBndRepository.SONATYPE_RELEASE_DIR + "/" + "biz_aQute_eval_"
46-
+ MavenBndRepository.SONATYPE_DEPLOYMENTID_FILE);
43+
File deploymentIDFile = new File(wsDir, MavenBndRepository.SONATYPE_RELEASE_DIR + "/"
44+
+ "biz_aQute_eval_" + MavenBndRepository.SONATYPE_DEPLOYMENTID_FILE);
4745
boolean remoteTest = true;
4846

4947
@BeforeAll
@@ -108,7 +106,6 @@ public void testSonatypeModeUnset() throws Exception {
108106
}
109107

110108
@Test
111-
@Disabled("Sonatype Snapshots needs debbugging")
112109
public void testReleaseUrlSnapshot() throws Exception {
113110
try (Workspace ws = Workspace.findWorkspace(wsDir)) {
114111
new File(wsDir, "cnf/ext/sonatype_release.bnd_").renameTo(sonatypeRepoFile);
@@ -119,13 +116,13 @@ public void testReleaseUrlSnapshot() throws Exception {
119116
}
120117

121118
@Test
122-
@Disabled("Sonatype Snapshots needs debbugging")
123119
public void testStagingUrlSnapshot() throws Exception {
124120
try (Workspace ws = Workspace.findWorkspace(wsDir)) {
125121
new File(wsDir, "cnf/ext/sonatype_staging.bnd_").renameTo(sonatypeRepoFile);
126122
assertTrue(sonatypeRepoFile.exists());
127123
}
128124
commentSnapshotLine();
125+
testDeployment(true);
129126
}
130127

131128
@Test

biz.aQute.repository/testresources/sonatype/cnf/ext/sonatype_release.bnd_

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# sonatype credentials from environment variables
1+
# sonatype credential from environment variables
22
sonatype_bearer: '${env;SONATYPE_BEARER;}'
33

44
# internet maven repo server
@@ -9,9 +9,6 @@ sonatype_snapshots: https://central.sonatype.com/repository/maven-snapshots
99

1010
-connection-settings.sonatype:\
1111
server; id = ${sonatype_release};\
12-
password = ${sonatype_bearer};\
13-
verify = false,\
14-
server; id = ${sonatype_snapshots};\
1512
password = ${sonatype_bearer};\
1613
verify = false
1714

biz.aQute.repository/testresources/sonatype/cnf/ext/sonatype_staging.bnd_

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ sonatype_snapshots: https://central.sonatype.com/repository/maven-snapshots
1010

1111
-connection-settings.sonatype:\
1212
server; id = ${sonatype_release};\
13-
password = ${sonatype_bearer};\
14-
verify = false,\
15-
server; id = ${sonatype_snapshots};\
1613
password = ${sonatype_bearer};\
1714
verify = false
1815

0 commit comments

Comments
 (0)