Skip to content

Commit 4a09f02

Browse files
committedJul 25, 2024
2.1.0 release
- multiple fixes - allow plugins access to complete pom xml
1 parent cf6848d commit 4a09f02

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed
 

‎src/main/java/JPM.java

+28-23
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import java.nio.file.Files;
1212
import java.nio.file.StandardCopyOption;
1313
import java.util.*;
14-
import java.util.function.Consumer;
14+
import java.util.concurrent.CopyOnWriteArrayList;
15+
import java.util.function.BiConsumer;
1516

1617
class ThisProject extends JPM.Project {
1718

@@ -46,7 +47,7 @@ class ThirdPartyPlugins extends JPM.Plugins{
4647
// (If you want to develop a plugin take a look at "JPM.Clean" class further below to get started)
4748
}
4849

49-
// 1JPM version 2.0.1 by Osiris-Team: https://github.com/Osiris-Team/1JPM
50+
// 1JPM version 2.1.0 by Osiris-Team: https://github.com/Osiris-Team/1JPM
5051
// To upgrade JPM, replace the JPM class below with its newer version
5152
public class JPM {
5253
public static final List<Plugin> plugins = new ArrayList<>();
@@ -93,6 +94,7 @@ private static void downloadMavenWrapper(File script) throws IOException {
9394

9495
System.out.println("Downloading file from: " + wrapperUrl);
9596
URL url = new URL(wrapperUrl);
97+
script.getParentFile().mkdirs();
9698
Files.copy(url.openStream(), script.toPath(), StandardCopyOption.REPLACE_EXISTING);
9799
}
98100

@@ -101,6 +103,7 @@ private static void downloadMavenWrapperJar(File jar) throws IOException {
101103

102104
System.out.println("Downloading file from: " + wrapperUrl);
103105
URL url = new URL(wrapperUrl);
106+
jar.getParentFile().mkdirs();
104107
Files.copy(url.openStream(), jar.toPath(), StandardCopyOption.REPLACE_EXISTING);
105108
}
106109

@@ -334,7 +337,7 @@ public static void main(String[] args) {
334337
}
335338

336339
public static class Plugin {
337-
public Consumer<Project> beforeGetConfiguration = (project) -> {};
340+
public List<BiConsumer<Project, XML>> beforeToXMLListeners = new CopyOnWriteArrayList<>();
338341
protected String groupId;
339342
protected String artifactId;
340343
protected String version;
@@ -352,11 +355,6 @@ public Plugin(String groupId, String artifactId, String version) {
352355
this.version = version;
353356
}
354357

355-
public Plugin withBeforeGetConfiguration(Consumer<Project> code){
356-
this.beforeGetConfiguration = code;
357-
return this;
358-
}
359-
360358
public void addConfiguration(String key, String value) {
361359
configuration.put(key, value);
362360
}
@@ -369,19 +367,26 @@ public void addDependency(Dependency dependency) {
369367
dependencies.add(dependency);
370368
}
371369

372-
private void executeBeforeGetConfiguration(Project project) {
373-
beforeGetConfiguration.accept(project);
370+
public Plugin onBeforeToXML(BiConsumer<Project, XML> code){
371+
beforeToXMLListeners.add(code);
372+
return this;
373+
}
374+
375+
private void executeBeforeToXML(Project project, XML projectXML) {
376+
for (BiConsumer<Project, XML> code : beforeToXMLListeners) {
377+
code.accept(project, projectXML);
378+
}
374379
}
375380

376-
private void executeAfterGetConfiguration(Project project) {
381+
private void executeAfterToXML(Project project) {
377382
configuration.clear();
378383
executions.clear();
379384
dependencies.clear();
380385
}
381386

382387

383-
public XML getConfiguration(Project project) {
384-
executeBeforeGetConfiguration(project);
388+
public XML toXML(Project project, XML projectXML) {
389+
executeBeforeToXML(project, projectXML);
385390

386391
// Create an XML object for the <plugin> element
387392
XML xml = new XML("plugin");
@@ -410,7 +415,7 @@ public XML getConfiguration(Project project) {
410415
}
411416
}
412417

413-
executeAfterGetConfiguration(project);
418+
executeAfterToXML(project);
414419
return xml;
415420
}
416421
}
@@ -552,7 +557,7 @@ public void generatePom() throws IOException {
552557
// Add <dependencyManagement> if there are managed dependencies
553558
if (!dependenciesManaged.isEmpty()) {
554559
for (Dependency dep : dependenciesManaged) {
555-
pom.add("dependencyManagement", dep.toXML());
560+
pom.add("dependencyManagement dependencies", dep.toXML());
556561
}
557562
}
558563

@@ -565,10 +570,10 @@ public void generatePom() throws IOException {
565570

566571
// Add <build> section with plugins and resources
567572
for (Plugin plugin : JPM.plugins) {
568-
pom.add("build plugins", plugin.getConfiguration(this));
573+
pom.add("build plugins", plugin.toXML(this, pom));
569574
}
570575
for (Plugin plugin : plugins) {
571-
pom.add("build plugins", plugin.getConfiguration(this));
576+
pom.add("build plugins", plugin.toXML(this, pom));
572577
}
573578

574579
// Add resources with a comment
@@ -592,7 +597,7 @@ public static class CompilerPlugin extends Plugin {
592597
public static CompilerPlugin get = new CompilerPlugin();
593598
public CompilerPlugin() {
594599
super("org.apache.maven.plugins", "maven-compiler-plugin", "3.8.1");
595-
withBeforeGetConfiguration(project -> {
600+
onBeforeToXML((project, pom) -> {
596601
addConfiguration("source", project.javaVersionSource);
597602
addConfiguration("target", project.javaVersionTarget);
598603

@@ -613,7 +618,7 @@ public static class JarPlugin extends Plugin {
613618
public static JarPlugin get = new JarPlugin();
614619
public JarPlugin() {
615620
super("org.apache.maven.plugins", "maven-jar-plugin", "3.2.0");
616-
withBeforeGetConfiguration(project -> {
621+
onBeforeToXML((project, pom) -> {
617622
addConfiguration("archive manifest addClasspath", "true");
618623
addConfiguration("archive manifest mainClass", project.mainClass);
619624
addConfiguration("finalName", project.jarName.replace(".jar", ""));
@@ -628,7 +633,7 @@ public static class AssemblyPlugin extends Plugin {
628633
public static AssemblyPlugin get = new AssemblyPlugin();
629634
public AssemblyPlugin() {
630635
super("org.apache.maven.plugins", "maven-assembly-plugin", "3.3.0");
631-
withBeforeGetConfiguration(project -> {
636+
onBeforeToXML((project, pom) -> {
632637
addConfiguration("descriptorRefs descriptorRef", "jar-with-dependencies");
633638
addConfiguration("archive manifest mainClass", project.mainClass);
634639
addConfiguration("finalName", project.fatJarName.replace(".jar", ""));
@@ -648,7 +653,7 @@ public static class SourcePlugin extends Plugin {
648653
public static SourcePlugin get = new SourcePlugin();
649654
public SourcePlugin() {
650655
super("org.apache.maven.plugins", "maven-source-plugin", "3.2.1");
651-
withBeforeGetConfiguration(project -> {
656+
onBeforeToXML((project, pom) -> {
652657
Execution execution = new Execution("attach-sources", null);
653658
addExecution(execution);
654659
execution.addGoal("jar");
@@ -663,7 +668,7 @@ public static class JavadocPlugin extends Plugin {
663668
public static JavadocPlugin get = new JavadocPlugin();
664669
public JavadocPlugin() {
665670
super("org.apache.maven.plugins", "maven-javadoc-plugin", "3.0.0");
666-
withBeforeGetConfiguration(project -> {
671+
onBeforeToXML((project, pom) -> {
667672
Execution execution = new Execution("resource-bundles", "package");
668673
addExecution(execution);
669674
execution.addGoal("resource-bundle");
@@ -681,7 +686,7 @@ public static class EnforcerPlugin extends Plugin {
681686
public static EnforcerPlugin get = new EnforcerPlugin();
682687
public EnforcerPlugin() {
683688
super("org.apache.maven.plugins", "maven-enforcer-plugin", "3.3.0");
684-
withBeforeGetConfiguration(project -> {
689+
onBeforeToXML((project, pom) -> {
685690
Execution execution = new Execution("enforce", null);
686691
addExecution(execution);
687692
execution.addGoal("enforce");

0 commit comments

Comments
 (0)
Please sign in to comment.