Skip to content

Commit 48a77d6

Browse files
authored
Use 'mvn-utils' version 1.0.0 (#23)
1 parent 9ec3144 commit 48a77d6

File tree

6 files changed

+12
-157
lines changed

6 files changed

+12
-157
lines changed

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ task javadocJar(type: Jar) {
4343
}
4444

4545
dependencies {
46+
implementation "fr.jmini.utils:mvn-utils:$mvnUtils"
4647
implementation "biz.aQute.bnd:biz.aQute.bnd:$bndVersion"
4748
implementation "com.google.code.gson:gson:$gsonVersion"
4849

gradle.properties

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ assertjVersion=3.11.1
77
bndVersion=5.3.0
88
junitVersion=5.7.1
99
gsonVersion=2.8.7
10+
mvnUtils=1.0.0
1011

1112
githubRepositoryOwner=jmini
1213
githubRepositoryName=ecentral

src/main/java/fr/jmini/utils/ecentral/Algorithm.java

-29
This file was deleted.

src/main/java/fr/jmini/utils/ecentral/ECentralTask.java

+8-70
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.nio.file.Files;
1313
import java.nio.file.Path;
1414
import java.nio.file.Paths;
15-
import java.security.MessageDigest;
1615
import java.util.Arrays;
1716
import java.util.List;
1817
import java.util.Objects;
@@ -25,6 +24,10 @@
2524
import com.google.gson.GsonBuilder;
2625
import com.google.gson.reflect.TypeToken;
2726

27+
import fr.jmini.utils.mvnutils.Algorithm;
28+
import fr.jmini.utils.mvnutils.Maven;
29+
import fr.jmini.utils.mvnutils.MavenArtifact;
30+
2831
public class ECentralTask {
2932

3033
private static final String GROUP_ID = "fr.jmini.ecentral";
@@ -43,7 +46,6 @@ public ECentralTask(Input input) {
4346

4447
public void run() {
4548
try {
46-
Files.createDirectories(getMavenBomFile().getParent());
4749
Files.createDirectories(getDataFolder());
4850
runInternal();
4951
} catch (IOException e) {
@@ -272,7 +274,7 @@ private void createMavenArtifacts() throws IOException {
272274
}
273275

274276
private static boolean checkArtifactInMavenCentral(MavenArtifact artifact) {
275-
String centralUrl = computeMavenCentralUrl(artifact);
277+
String centralUrl = Maven.jarMavenCentralUrl(artifact);
276278
try {
277279
HttpURLConnection connection = (HttpURLConnection) new URL(centralUrl).openConnection();
278280
connection.setRequestMethod("HEAD");
@@ -283,49 +285,10 @@ private static boolean checkArtifactInMavenCentral(MavenArtifact artifact) {
283285
}
284286
}
285287

286-
static String computeMavenCentralUrl(MavenArtifact artifact) {
287-
return computeMavenCentralUrl(artifact, ".jar");
288-
}
289-
290-
static String computeMavenCentralUrl(MavenArtifact artifact, String extension) {
291-
// See https://github.com/eclipse/aether-core/blob/aether-0.9.1.v20140329/aether-util/src/main/java/org/eclipse/aether/util/repository/layout/MavenDefaultLayout.java#L42
292-
293-
StringBuilder sb = new StringBuilder();
294-
sb.append("https://repo1.maven.org/maven2/");
295-
sb.append(subPathInMavenRepo(artifact, extension));
296-
return sb.toString();
297-
}
298-
299-
private static String subPathInMavenRepo(MavenArtifact artifact, String extension) {
300-
StringBuilder sb = new StringBuilder();
301-
sb.append(artifact.getGroupId()
302-
.replace('.', '/'));
303-
sb.append('/');
304-
sb.append(artifact.getArtifactId());
305-
sb.append('/');
306-
sb.append(artifact.getVersion());
307-
sb.append('/');
308-
sb.append(artifact.getArtifactId());
309-
sb.append('-');
310-
sb.append(artifact.getVersion());
311-
sb.append(extension);
312-
return sb.toString();
313-
}
314-
315288
private void createMavenBomFile() throws IOException {
316289
List<MavenArtifact> entries = parseArtifactsFile(getMavenArtifactsFile());
317290
String content = createMavenBomContent(entries);
318-
Files.writeString(getMavenBomFile(), content, StandardCharsets.UTF_8);
319-
320-
writeHash(content, Algorithm.MD_5);
321-
writeHash(content, Algorithm.SHA_1);
322-
writeHash(content, Algorithm.SHA_256);
323-
writeHash(content, Algorithm.SHA_512);
324-
}
325-
326-
private void writeHash(String content, Algorithm algorithm) throws IOException {
327-
String hash = calculateHash(content, algorithm);
328-
Files.writeString(getMavenBomFile(".pom" + algorithm.getExtension()), hash, StandardCharsets.UTF_8);
291+
Maven.writeFileToRepositoryWithArmoredFiles(Paths.get("repo"), getBomArtifact(), ".pom", content, Algorithm.MD_5, Algorithm.SHA_1, Algorithm.SHA_256, Algorithm.SHA_512);
329292
}
330293

331294
private String createMavenBomContent(List<MavenArtifact> entries) {
@@ -376,25 +339,6 @@ private String createMavenBomContent(List<MavenArtifact> entries) {
376339
return sb.toString();
377340
}
378341

379-
static String calculateHash(String content, Algorithm algorithm) {
380-
MessageDigest digest = algorithm.getMessageDigest();
381-
byte[] encodedhash = digest.digest(
382-
content.getBytes(StandardCharsets.UTF_8));
383-
return bytesToHex(encodedhash);
384-
}
385-
386-
private static String bytesToHex(byte[] hash) {
387-
StringBuilder hexString = new StringBuilder(2 * hash.length);
388-
for (int i = 0; i < hash.length; i++) {
389-
String hex = Integer.toHexString(0xff & hash[i]);
390-
if (hex.length() == 1) {
391-
hexString.append('0');
392-
}
393-
hexString.append(hex);
394-
}
395-
return hexString.toString();
396-
}
397-
398342
private void writeArtifactsToFile(Path file, List<MavenArtifact> artifacts) throws IOException {
399343
Gson gson = new GsonBuilder()
400344
.setPrettyPrinting()
@@ -425,13 +369,7 @@ private Path getMavenArtifactsFile() {
425369
return getDataFolder().resolve("maven-artifacts.json");
426370
}
427371

428-
private Path getMavenBomFile() {
429-
return getMavenBomFile(".pom");
430-
}
431-
432-
private Path getMavenBomFile(String extension) {
433-
MavenArtifact artifact = new MavenArtifact(GROUP_ID, ARTIFACT_ID, input.getReleaseVersion());
434-
return Paths.get("repo")
435-
.resolve(subPathInMavenRepo(artifact, extension));
372+
private MavenArtifact getBomArtifact() {
373+
return new MavenArtifact(GROUP_ID, ARTIFACT_ID, input.getReleaseVersion());
436374
}
437375
}

src/main/java/fr/jmini/utils/ecentral/MavenArtifact.java

-27
This file was deleted.

src/test/java/fr/jmini/utils/ecentral/ECentralTaskTest.java

+2-31
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import org.junit.jupiter.api.Test;
99

10+
import fr.jmini.utils.mvnutils.MavenArtifact;
11+
1012
class ECentralTaskTest {
1113
@Test
1214
void testParseBndEntry() throws Exception {
@@ -233,35 +235,4 @@ void testConvertVersion() throws Exception {
233235
.isEqualTo("4.12.0");
234236
}
235237

236-
@Test
237-
void testComputeMavenCentralUrl() throws Exception {
238-
MavenArtifact a = new MavenArtifact("org.eclipse.platform", "org.eclipse.ant.core", "3.5.600");
239-
assertThat(ECentralTask.computeMavenCentralUrl(a))
240-
.as("url of the jar in maven central")
241-
.isEqualTo("https://repo1.maven.org/maven2/org/eclipse/platform/org.eclipse.ant.core/3.5.600/org.eclipse.ant.core-3.5.600.jar");
242-
243-
assertThat(ECentralTask.computeMavenCentralUrl(a, ".pom"))
244-
.as("url of the pom in maven central")
245-
.isEqualTo("https://repo1.maven.org/maven2/org/eclipse/platform/org.eclipse.ant.core/3.5.600/org.eclipse.ant.core-3.5.600.pom");
246-
247-
assertThat(ECentralTask.computeMavenCentralUrl(a, ".jar.asc"))
248-
.as("url of the armored ASCII file of the jar in maven central")
249-
.isEqualTo("https://repo1.maven.org/maven2/org/eclipse/platform/org.eclipse.ant.core/3.5.600/org.eclipse.ant.core-3.5.600.jar.asc");
250-
251-
}
252-
253-
@Test
254-
void testCalculateHash() throws Exception {
255-
String md5 = ECentralTask.calculateHash("test", Algorithm.MD_5);
256-
assertThat(md5).isEqualTo("098f6bcd4621d373cade4e832627b4f6");
257-
258-
String sha1 = ECentralTask.calculateHash("test", Algorithm.SHA_1);
259-
assertThat(sha1).isEqualTo("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3");
260-
261-
String sha256 = ECentralTask.calculateHash("test", Algorithm.SHA_256);
262-
assertThat(sha256).isEqualTo("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08");
263-
264-
String sha512 = ECentralTask.calculateHash("test", Algorithm.SHA_512);
265-
assertThat(sha512).isEqualTo("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff");
266-
}
267238
}

0 commit comments

Comments
 (0)