Skip to content

Commit d7871e2

Browse files
initial commit
1 parent 584bead commit d7871e2

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ buildNumber.properties
99
.mvn/timing.properties
1010
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
1111
.mvn/wrapper/maven-wrapper.jar
12+
.idea/
13+
*.iml

pom.xml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>io.github.makingthematrix</groupId>
5+
<artifactId>scala-suffix-maven-plugin</artifactId>
6+
<packaging>maven-plugin</packaging>
7+
<version>0.0.3</version>
8+
<name>Scala SufFix Maven Plugin</name>
9+
<url>https://github.com/makingthematrix/scala-suffix</url>
10+
<organization>
11+
<name>makingthematrix</name>
12+
<url>https://github.com/makingthematrix</url>
13+
</organization>
14+
15+
<properties>
16+
<maven-compiler-plugin-version>3.8.1</maven-compiler-plugin-version>
17+
<maven.compiler.source>11</maven.compiler.source>
18+
<maven.compiler.target>11</maven.compiler.target>
19+
</properties>
20+
21+
<build>
22+
<plugins>
23+
<plugin>
24+
<groupId>org.apache.maven.plugins</groupId>
25+
<artifactId>maven-compiler-plugin</artifactId>
26+
<version>${maven-compiler-plugin-version}</version>
27+
</plugin>
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-plugin-plugin</artifactId>
31+
<version>3.6.1</version>
32+
</plugin>
33+
</plugins>
34+
<defaultGoal>suffix</defaultGoal>
35+
</build>
36+
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.apache.maven</groupId>
40+
<artifactId>maven-plugin-api</artifactId>
41+
<version>3.8.1</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.apache.maven.plugin-tools</groupId>
45+
<artifactId>maven-plugin-annotations</artifactId>
46+
<version>3.6.0</version>
47+
<scope>provided</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.apache.maven</groupId>
51+
<artifactId>maven-project</artifactId>
52+
<version>2.2.1</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>net.lingala.zip4j</groupId>
56+
<artifactId>zip4j</artifactId>
57+
<version>2.7.0</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>commons-io</groupId>
61+
<artifactId>commons-io</artifactId>
62+
<version>2.8.0</version>
63+
</dependency>
64+
<!-- https://mvnrepository.com/artifact/io.vavr/vavr -->
65+
<dependency>
66+
<groupId>io.vavr</groupId>
67+
<artifactId>vavr</artifactId>
68+
<version>0.10.3</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.junit</groupId>
72+
<artifactId>junit-bom</artifactId>
73+
<version>5.7.1</version>
74+
<type>pom</type>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.jetbrains</groupId>
79+
<artifactId>annotations</artifactId>
80+
<version>20.1.0</version>
81+
<scope>compile</scope>
82+
</dependency>
83+
</dependencies>
84+
</project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package io.github.makingthematrix;
2+
3+
import io.vavr.Lazy;
4+
import net.lingala.zip4j.ZipFile;
5+
import net.lingala.zip4j.model.ZipParameters;
6+
import org.apache.commons.io.FileUtils;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.nio.charset.Charset;
12+
import java.util.List;
13+
import java.util.stream.Collectors;
14+
15+
final class ManifestToFix {
16+
private static final String AUTOMATIC_MODULE_NAME = "Automatic-Module-Name";
17+
private static final String MANIFEST_MF = "META-INF/MANIFEST.MF";
18+
19+
private static final Lazy<ZipParameters> zipParams = Lazy.of(() -> {
20+
final var params = new ZipParameters();
21+
params.setIncludeRootFolder(true);
22+
params.setOverrideExistingFilesInZip(true);
23+
params.setFileNameInZip(MANIFEST_MF);
24+
return params;
25+
});
26+
27+
private final File manifestFile;
28+
private final ZipFile zipFile;
29+
30+
public ManifestToFix(@NotNull final File artifactFile, @NotNull final File outputDir) throws IOException {
31+
this.zipFile = new ZipFile(artifactFile);
32+
zipFile.extractFile(MANIFEST_MF, outputDir.getAbsolutePath());
33+
this.manifestFile = new File(outputDir, MANIFEST_MF);
34+
}
35+
36+
public boolean fixLib(@NotNull String newLibraryName) throws IOException {
37+
if (manifestFile.exists()) {
38+
return fixManifestFile(newLibraryName);
39+
} else {
40+
throw new IOException("No file " + manifestFile.getAbsolutePath() + " found");
41+
}
42+
}
43+
44+
private boolean fixManifestFile(String newLibraryName) throws IOException {
45+
final var manifestLines = FileUtils.readLines(manifestFile, Charset.defaultCharset());
46+
final var moduleNameLine =
47+
manifestLines.stream()
48+
.filter(line -> line.toLowerCase().contains(AUTOMATIC_MODULE_NAME.toLowerCase()))
49+
.findAny();
50+
51+
if (moduleNameLine.isPresent()) {
52+
return false;
53+
}
54+
55+
addAutomaticModuleName(manifestLines, newLibraryName);
56+
if (!zipFile.isValidZipFile()) {
57+
throw new IOException("After the operation the zip file is INVALID: " + zipFile.getFile().getAbsolutePath());
58+
}
59+
return true;
60+
}
61+
62+
private void addAutomaticModuleName(List<String> manifestLines, String newLibraryName) throws IOException {
63+
final var newManifestLines =
64+
List.copyOf(manifestLines).stream()
65+
.map(String::trim)
66+
.filter(line -> !line.isBlank())
67+
.collect(Collectors.toList());
68+
newManifestLines.add(AUTOMATIC_MODULE_NAME + ": " + newLibraryName);
69+
70+
manifestFile.createNewFile();
71+
FileUtils.writeLines(manifestFile, newManifestLines);
72+
zipFile.addFile(manifestFile, zipParams.get());
73+
}
74+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package io.github.makingthematrix;
2+
3+
/*
4+
* Copyright 2001-2005 The Apache Software Foundation.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import io.vavr.Lazy;
20+
import io.vavr.Tuple;
21+
import io.vavr.Tuple2;
22+
import org.apache.commons.io.FileUtils;
23+
import org.apache.maven.artifact.Artifact;
24+
import org.apache.maven.plugin.AbstractMojo;
25+
import org.apache.maven.plugins.annotations.LifecyclePhase;
26+
import org.apache.maven.plugins.annotations.Mojo;
27+
import org.apache.maven.plugins.annotations.Parameter;
28+
import org.apache.maven.project.MavenProject;
29+
30+
import java.io.File;
31+
import java.io.IOException;
32+
import java.util.Collections;
33+
import java.util.Optional;
34+
import java.util.Set;
35+
36+
/**
37+
* Modifies MANIFEST.MF in the given Scala dependencies, removing the Scala version suffix from the module name
38+
*
39+
*/
40+
@Mojo(name = "suffix", defaultPhase = LifecyclePhase.INITIALIZE)
41+
public final class SufFixMojo extends AbstractMojo {
42+
@Parameter(defaultValue = "${project}", required = true, readonly = true)
43+
private MavenProject project;
44+
45+
@Parameter(property = "libraries", required = true, readonly = true)
46+
private Set<String> libraries;
47+
48+
private final Lazy<File> tempDir = Lazy.of(() -> {
49+
final var temp = new File(FileUtils.getTempDirectory(), "scala-suffix-" + System.currentTimeMillis());
50+
if (!temp.mkdir()) {
51+
error("Unable to create a temp directory: " + temp.getAbsolutePath());
52+
} else {
53+
temp.deleteOnExit();
54+
}
55+
return temp;
56+
});
57+
58+
public void execute() {
59+
final var artifacts = (Set<Artifact>)Collections.unmodifiableSet(project.getDependencyArtifacts());
60+
libraries.stream().map(newLibraryName ->
61+
artifacts.stream()
62+
.filter(art -> art.getArtifactId().toLowerCase().contains(newLibraryName))
63+
.findAny()
64+
.map(art -> Tuple.of(art.getFile(), newLibraryName))
65+
).filter(Optional::isPresent)
66+
.map(Optional::get)
67+
.forEach(this::fixManifest);
68+
}
69+
70+
private void fixManifest(Tuple2<File, String> tuple) {
71+
clean();
72+
try {
73+
final var manifestToFix = new ManifestToFix(tuple._1, tempDir.get());
74+
if (manifestToFix.fixLib(tuple._2)) {
75+
info("The manifest file fixed for: " + tuple._2);
76+
}
77+
} catch (IOException ex) {
78+
error(ex);
79+
} finally {
80+
clean();
81+
}
82+
}
83+
84+
private void clean() {
85+
try {
86+
FileUtils.cleanDirectory(tempDir.get());
87+
} catch (IOException ex) {
88+
error(ex);
89+
}
90+
}
91+
92+
private void info(String str) {
93+
getLog().info(str);
94+
}
95+
96+
private void error(String str) {
97+
getLog().error(str);
98+
}
99+
100+
private void error(Throwable t) {
101+
getLog().error(t);
102+
}
103+
}

0 commit comments

Comments
 (0)