Skip to content

Commit 729a150

Browse files
committed
Tune it
1 parent f6e8956 commit 729a150

File tree

17 files changed

+306
-167
lines changed

17 files changed

+306
-167
lines changed

core/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,24 @@
2525
<dependency>
2626
<groupId>org.slf4j</groupId>
2727
<artifactId>slf4j-api</artifactId>
28-
<scope>provided</scope>
2928
</dependency>
3029
<dependency>
3130
<groupId>javax.inject</groupId>
3231
<artifactId>javax.inject</artifactId>
33-
<scope>provided</scope>
3432
</dependency>
3533

3634
<!-- Resolver -->
3735
<dependency>
3836
<groupId>org.apache.maven.resolver</groupId>
3937
<artifactId>maven-resolver-api</artifactId>
40-
<scope>provided</scope>
4138
</dependency>
4239
<dependency>
4340
<groupId>org.apache.maven.resolver</groupId>
4441
<artifactId>maven-resolver-spi</artifactId>
45-
<scope>provided</scope>
4642
</dependency>
4743
<dependency>
4844
<groupId>org.apache.maven.resolver</groupId>
4945
<artifactId>maven-resolver-util</artifactId>
50-
<scope>provided</scope>
5146
</dependency>
5247

5348
<dependency>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (c) 2023-2024 Maveniverse Org.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-v20.html
7+
*/
8+
package eu.maveniverse.maven.njord.shared.impl.publisher;
9+
10+
import static java.util.Objects.requireNonNull;
11+
12+
import eu.maveniverse.maven.njord.shared.impl.CloseableSupport;
13+
import eu.maveniverse.maven.njord.shared.publisher.ArtifactStorePublisher;
14+
import eu.maveniverse.maven.njord.shared.publisher.ArtifactStoreValidator;
15+
import eu.maveniverse.maven.njord.shared.store.ArtifactStore;
16+
import eu.maveniverse.maven.njord.shared.store.RepositoryMode;
17+
import java.io.IOException;
18+
import java.util.Optional;
19+
import java.util.concurrent.atomic.AtomicInteger;
20+
import org.eclipse.aether.RepositorySystem;
21+
import org.eclipse.aether.RepositorySystemSession;
22+
import org.eclipse.aether.repository.RemoteRepository;
23+
24+
public abstract class ArtifactStorePublisherSupport extends CloseableSupport implements ArtifactStorePublisher {
25+
protected final RepositorySystem repositorySystem;
26+
protected final RepositorySystemSession session;
27+
protected final String name;
28+
protected final String description;
29+
protected final RemoteRepository targetReleaseRepository;
30+
protected final RemoteRepository targetSnapshotRepository;
31+
protected final RemoteRepository serviceReleaseRepository;
32+
protected final RemoteRepository serviceSnapshotRepository;
33+
34+
protected ArtifactStorePublisherSupport(
35+
RepositorySystem repositorySystem,
36+
RepositorySystemSession session,
37+
String name,
38+
String description,
39+
RemoteRepository targetReleaseRepository,
40+
RemoteRepository targetSnapshotRepository,
41+
RemoteRepository serviceReleaseRepository,
42+
RemoteRepository serviceSnapshotRepository) {
43+
this.repositorySystem = requireNonNull(repositorySystem);
44+
this.session = requireNonNull(session);
45+
this.name = requireNonNull(name);
46+
this.description = requireNonNull(description);
47+
this.targetReleaseRepository = targetReleaseRepository;
48+
this.targetSnapshotRepository = targetSnapshotRepository;
49+
this.serviceReleaseRepository = serviceReleaseRepository;
50+
this.serviceSnapshotRepository = serviceSnapshotRepository;
51+
}
52+
53+
@Override
54+
public String name() {
55+
return name;
56+
}
57+
58+
@Override
59+
public String description() {
60+
return description;
61+
}
62+
63+
@Override
64+
public Optional<RemoteRepository> targetReleaseRepository() {
65+
return Optional.ofNullable(targetReleaseRepository);
66+
}
67+
68+
@Override
69+
public Optional<RemoteRepository> targetSnapshotRepository() {
70+
return Optional.ofNullable(targetSnapshotRepository);
71+
}
72+
73+
@Override
74+
public Optional<RemoteRepository> serviceReleaseRepository() {
75+
return Optional.ofNullable(serviceReleaseRepository);
76+
}
77+
78+
@Override
79+
public Optional<RemoteRepository> serviceSnapshotRepository() {
80+
return Optional.ofNullable(serviceSnapshotRepository);
81+
}
82+
83+
@Override
84+
public Optional<ArtifactStoreValidator> releaseValidator() {
85+
return Optional.empty();
86+
}
87+
88+
@Override
89+
public Optional<ArtifactStoreValidator> snapshotValidator() {
90+
return Optional.empty();
91+
}
92+
93+
@Override
94+
public abstract void publish(ArtifactStore artifactStore) throws IOException;
95+
96+
protected void validateArtifactStore(ArtifactStore artifactStore) throws IOException {
97+
Optional<ArtifactStoreValidator> vo =
98+
artifactStore.repositoryMode() == RepositoryMode.RELEASE ? releaseValidator() : snapshotValidator();
99+
if (vo.isPresent()) {
100+
ArtifactStoreValidator.ValidationResult vr = vo.orElseThrow().validate(artifactStore);
101+
logger.error("ArtifactStore {} validation result:", artifactStore);
102+
AtomicInteger counter = new AtomicInteger(0);
103+
for (String msg : vr.error()) {
104+
logger.error(" {}. {}", counter.incrementAndGet(), msg);
105+
}
106+
for (String msg : vr.warning()) {
107+
logger.warn(" {}. {}", counter.incrementAndGet(), msg);
108+
}
109+
for (String msg : vr.info()) {
110+
logger.info(" {}. {}", counter.incrementAndGet(), msg);
111+
}
112+
if (!vr.isValid()) {
113+
logger.error("ArtifactStore {} failed validation", artifactStore);
114+
throw new IOException("Validation failed");
115+
}
116+
}
117+
}
118+
}

core/src/main/java/eu/maveniverse/maven/njord/shared/impl/repository/DefaultArtifactStore.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,37 +127,31 @@ public DefaultArtifactStore(
127127

128128
@Override
129129
public String name() {
130-
checkClosed();
131130
return name;
132131
}
133132

134133
@Override
135134
public Instant created() {
136-
checkClosed();
137135
return created;
138136
}
139137

140138
@Override
141139
public RepositoryMode repositoryMode() {
142-
checkClosed();
143140
return repositoryMode;
144141
}
145142

146143
@Override
147144
public boolean allowRedeploy() {
148-
checkClosed();
149145
return allowRedeploy;
150146
}
151147

152148
@Override
153149
public List<ChecksumAlgorithmFactory> checksumAlgorithmFactories() {
154-
checkClosed();
155150
return checksumAlgorithmFactories;
156151
}
157152

158153
@Override
159154
public List<String> omitChecksumsForExtensions() {
160-
checkClosed();
161155
return omitChecksumsForExtensions;
162156
}
163157

@@ -275,11 +269,21 @@ public void close() throws IOException {
275269
};
276270
}
277271

272+
@Override
273+
protected void doClose() throws IOException {
274+
DirectoryLocker.INSTANCE.unlockDirectory(basedir);
275+
}
276+
278277
@Override
279278
public String toString() {
280-
return String.format(
281-
"%s (%s, %s, %s artifacts)",
282-
name(), created(), repositoryMode().name(), artifacts().size());
279+
if (closed.get()) {
280+
return String.format(
281+
"%s (%s, %s, closed)", name(), created(), repositoryMode().name());
282+
} else {
283+
return String.format(
284+
"%s (%s, %s, %s artifacts)",
285+
name(), created(), repositoryMode().name(), artifacts().size());
286+
}
283287
}
284288

285289
private String metadataPath(Metadata metadata) {
@@ -326,12 +330,14 @@ private <E> Collection<E> readIndex(String what, Function<String, E> transform)
326330
}
327331

328332
private <E> void appendIndex(String what, Collection<E> entries, Function<E, String> transform) throws IOException {
329-
Path index = basedir.resolve(".meta").resolve(what);
330-
if (!entries.isEmpty() && !Files.isRegularFile(index)) {
331-
Files.createDirectories(index.getParent());
332-
Files.createFile(index);
333+
if (!entries.isEmpty()) {
334+
Path index = basedir.resolve(".meta").resolve(what);
335+
if (!Files.isRegularFile(index)) {
336+
Files.createDirectories(index.getParent());
337+
Files.createFile(index);
338+
}
339+
List<String> lines = entries.stream().map(transform).toList();
340+
Files.write(index, lines, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
333341
}
334-
List<String> lines = entries.stream().map(transform).toList();
335-
Files.write(index, lines, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
336342
}
337343
}

core/src/main/java/eu/maveniverse/maven/njord/shared/impl/repository/DefaultArtifactStoreExporter.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public DefaultArtifactStoreExporter(Config config) {
2727
}
2828

2929
@Override
30-
public void exportAsDirectory(ArtifactStore artifactStore, Path directory) throws IOException {
30+
public Path exportAsDirectory(ArtifactStore artifactStore, Path directory) throws IOException {
3131
requireNonNull(artifactStore);
3232
requireNonNull(directory);
3333
checkClosed();
@@ -36,16 +36,14 @@ public void exportAsDirectory(ArtifactStore artifactStore, Path directory) throw
3636
if (Files.exists(targetDirectory)) {
3737
throw new IOException("Exporting to existing directory not supported");
3838
}
39-
try (ArtifactStore store = artifactStore) {
40-
logger.info("Exporting ArtifactStore {} to {} as directory", store, targetDirectory);
41-
FileUtils.copyRecursively(store.basedir(), targetDirectory, p -> !p.getFileName()
42-
.toString()
43-
.startsWith("."));
44-
}
39+
FileUtils.copyRecursively(artifactStore.basedir(), targetDirectory, p -> !p.getFileName()
40+
.toString()
41+
.startsWith("."));
42+
return targetDirectory;
4543
}
4644

4745
@Override
48-
public void exportAsBundle(ArtifactStore artifactStore, Path directory) throws IOException {
46+
public Path exportAsBundle(ArtifactStore artifactStore, Path directory) throws IOException {
4947
requireNonNull(artifactStore);
5048
requireNonNull(directory);
5149
checkClosed();
@@ -60,11 +58,10 @@ public void exportAsBundle(ArtifactStore artifactStore, Path directory) throws I
6058
}
6159
try (FileSystem fs = FileSystems.newFileSystem(bundleFile, Map.of("create", "true"), null)) {
6260
Path root = fs.getPath("/");
63-
try (ArtifactStore store = artifactStore) {
64-
logger.info("Exporting ArtifactStore {} to {} as bundle", store, bundleFile);
65-
FileUtils.copyRecursively(
66-
store.basedir(), root, p -> !p.getFileName().toString().startsWith("."));
67-
}
61+
FileUtils.copyRecursively(artifactStore.basedir(), root, p -> !p.getFileName()
62+
.toString()
63+
.startsWith("."));
6864
}
65+
return bundleFile;
6966
}
7067
}

core/src/main/java/eu/maveniverse/maven/njord/shared/publisher/ArtifactStorePublisher.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ public interface ArtifactStorePublisher {
4444
Optional<RemoteRepository> serviceSnapshotRepository();
4545

4646
/**
47-
* The validator that must be applied to store before publishing.
47+
* The validator that must be applied to release store before publishing.
4848
*/
49-
Optional<ArtifactStoreValidator> validator();
49+
Optional<ArtifactStoreValidator> releaseValidator();
50+
51+
/**
52+
* The validator that must be applied to snapshot store before publishing.
53+
*/
54+
Optional<ArtifactStoreValidator> snapshotValidator();
5055

5156
/**
5257
* Performs the publishing.

core/src/main/java/eu/maveniverse/maven/njord/shared/store/ArtifactStoreExporter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
public interface ArtifactStoreExporter extends Closeable {
1515
/**
16-
* Exports store as directory hierarchy using Maven remote repository layout.
16+
* Exports store as directory hierarchy using Maven remote repository layout. Returns the root directory.
1717
*/
18-
void exportAsDirectory(ArtifactStore artifactStore, Path outputDirectory) throws IOException;
18+
Path exportAsDirectory(ArtifactStore artifactStore, Path outputDirectory) throws IOException;
1919

2020
/**
21-
* Exports store as ZIP bundle.
21+
* Exports store as ZIP bundle. Returns the ZIP file.
2222
*/
23-
void exportAsBundle(ArtifactStore artifactStore, Path outputDirectory) throws IOException;
23+
Path exportAsBundle(ArtifactStore artifactStore, Path outputDirectory) throws IOException;
2424
}

extension/src/main/java/eu/maveniverse/maven/njord/extension3/NjordRepositoryConnector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ public void put(
7474
} catch (IOException e) {
7575
if (artifactUploads != null) {
7676
artifactUploads.stream()
77-
.filter(u -> u.getException() != null)
77+
.filter(u -> u.getException() == null)
7878
.forEach(u ->
7979
u.setException(new ArtifactTransferException(u.getArtifact(), remoteRepository, e)));
8080
}
8181
if (metadataUploads != null) {
8282
metadataUploads.stream()
83-
.filter(u -> u.getException() != null)
83+
.filter(u -> u.getException() == null)
8484
.forEach(u ->
8585
u.setException(new MetadataTransferException(u.getMetadata(), remoteRepository, e)));
8686
}

plugin/src/main/java/eu/maveniverse/maven/njord/plugin3/ExportBundleMojo.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ protected void doExecute(NjordSession ns) throws IOException, MojoExecutionExcep
3939
if (!Files.isDirectory(targetDirectory)) {
4040
Files.createDirectories(targetDirectory);
4141
}
42+
Path result;
43+
logger.info("Exporting store {} as bundle to {}", store, directory);
4244
try (ArtifactStoreExporter artifactStoreExporter = ns.createArtifactStoreExporter()) {
43-
artifactStoreExporter.exportAsBundle(storeOptional.orElseThrow(), targetDirectory);
45+
result = artifactStoreExporter.exportAsBundle(storeOptional.orElseThrow(), targetDirectory);
4446
}
47+
logger.info("Exported to " + result);
4548
} else {
4649
logger.warn("ArtifactStore with given name not found");
4750
}

plugin/src/main/java/eu/maveniverse/maven/njord/plugin3/ExportMojo.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ protected void doExecute(NjordSession ns) throws IOException, MojoExecutionExcep
3838
if (Files.exists(targetDirectory)) {
3939
throw new MojoExecutionException("Exporting to existing directory not supported");
4040
}
41+
Path result;
42+
logger.info("Exporting store {} as directory to {}", store, directory);
4143
try (ArtifactStoreExporter artifactStoreExporter = ns.createArtifactStoreExporter()) {
42-
artifactStoreExporter.exportAsDirectory(storeOptional.orElseThrow(), targetDirectory);
44+
result = artifactStoreExporter.exportAsDirectory(storeOptional.orElseThrow(), targetDirectory);
4345
}
46+
logger.info("Exported to " + result);
4447
} else {
4548
logger.warn("ArtifactStore with given name not found");
4649
}

plugin/src/main/java/eu/maveniverse/maven/njord/plugin3/PublishMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class PublishMojo extends NjordMojoSupport {
2727
@Parameter(required = true, property = "target")
2828
private String target;
2929

30-
@Parameter(required = true, property = "drop", defaultValue = "true")
30+
@Parameter(required = true, property = "drop", defaultValue = "false")
3131
private boolean drop;
3232

3333
@Override

0 commit comments

Comments
 (0)