11
11
import java .nio .file .Files ;
12
12
import java .nio .file .StandardCopyOption ;
13
13
import java .util .*;
14
- import java .util .function .Consumer ;
14
+ import java .util .concurrent .CopyOnWriteArrayList ;
15
+ import java .util .function .BiConsumer ;
15
16
16
17
class ThisProject extends JPM .Project {
17
18
@@ -46,7 +47,7 @@ class ThirdPartyPlugins extends JPM.Plugins{
46
47
// (If you want to develop a plugin take a look at "JPM.Clean" class further below to get started)
47
48
}
48
49
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
50
51
// To upgrade JPM, replace the JPM class below with its newer version
51
52
public class JPM {
52
53
public static final List <Plugin > plugins = new ArrayList <>();
@@ -93,6 +94,7 @@ private static void downloadMavenWrapper(File script) throws IOException {
93
94
94
95
System .out .println ("Downloading file from: " + wrapperUrl );
95
96
URL url = new URL (wrapperUrl );
97
+ script .getParentFile ().mkdirs ();
96
98
Files .copy (url .openStream (), script .toPath (), StandardCopyOption .REPLACE_EXISTING );
97
99
}
98
100
@@ -101,6 +103,7 @@ private static void downloadMavenWrapperJar(File jar) throws IOException {
101
103
102
104
System .out .println ("Downloading file from: " + wrapperUrl );
103
105
URL url = new URL (wrapperUrl );
106
+ jar .getParentFile ().mkdirs ();
104
107
Files .copy (url .openStream (), jar .toPath (), StandardCopyOption .REPLACE_EXISTING );
105
108
}
106
109
@@ -334,7 +337,7 @@ public static void main(String[] args) {
334
337
}
335
338
336
339
public static class Plugin {
337
- public Consumer < Project > beforeGetConfiguration = ( project ) -> {} ;
340
+ public List < BiConsumer < Project , XML >> beforeToXMLListeners = new CopyOnWriteArrayList <>() ;
338
341
protected String groupId ;
339
342
protected String artifactId ;
340
343
protected String version ;
@@ -352,11 +355,6 @@ public Plugin(String groupId, String artifactId, String version) {
352
355
this .version = version ;
353
356
}
354
357
355
- public Plugin withBeforeGetConfiguration (Consumer <Project > code ){
356
- this .beforeGetConfiguration = code ;
357
- return this ;
358
- }
359
-
360
358
public void addConfiguration (String key , String value ) {
361
359
configuration .put (key , value );
362
360
}
@@ -369,19 +367,26 @@ public void addDependency(Dependency dependency) {
369
367
dependencies .add (dependency );
370
368
}
371
369
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
+ }
374
379
}
375
380
376
- private void executeAfterGetConfiguration (Project project ) {
381
+ private void executeAfterToXML (Project project ) {
377
382
configuration .clear ();
378
383
executions .clear ();
379
384
dependencies .clear ();
380
385
}
381
386
382
387
383
- public XML getConfiguration (Project project ) {
384
- executeBeforeGetConfiguration (project );
388
+ public XML toXML (Project project , XML projectXML ) {
389
+ executeBeforeToXML (project , projectXML );
385
390
386
391
// Create an XML object for the <plugin> element
387
392
XML xml = new XML ("plugin" );
@@ -410,7 +415,7 @@ public XML getConfiguration(Project project) {
410
415
}
411
416
}
412
417
413
- executeAfterGetConfiguration (project );
418
+ executeAfterToXML (project );
414
419
return xml ;
415
420
}
416
421
}
@@ -552,7 +557,7 @@ public void generatePom() throws IOException {
552
557
// Add <dependencyManagement> if there are managed dependencies
553
558
if (!dependenciesManaged .isEmpty ()) {
554
559
for (Dependency dep : dependenciesManaged ) {
555
- pom .add ("dependencyManagement" , dep .toXML ());
560
+ pom .add ("dependencyManagement dependencies " , dep .toXML ());
556
561
}
557
562
}
558
563
@@ -565,10 +570,10 @@ public void generatePom() throws IOException {
565
570
566
571
// Add <build> section with plugins and resources
567
572
for (Plugin plugin : JPM .plugins ) {
568
- pom .add ("build plugins" , plugin .getConfiguration (this ));
573
+ pom .add ("build plugins" , plugin .toXML (this , pom ));
569
574
}
570
575
for (Plugin plugin : plugins ) {
571
- pom .add ("build plugins" , plugin .getConfiguration (this ));
576
+ pom .add ("build plugins" , plugin .toXML (this , pom ));
572
577
}
573
578
574
579
// Add resources with a comment
@@ -592,7 +597,7 @@ public static class CompilerPlugin extends Plugin {
592
597
public static CompilerPlugin get = new CompilerPlugin ();
593
598
public CompilerPlugin () {
594
599
super ("org.apache.maven.plugins" , "maven-compiler-plugin" , "3.8.1" );
595
- withBeforeGetConfiguration ( project -> {
600
+ onBeforeToXML (( project , pom ) -> {
596
601
addConfiguration ("source" , project .javaVersionSource );
597
602
addConfiguration ("target" , project .javaVersionTarget );
598
603
@@ -613,7 +618,7 @@ public static class JarPlugin extends Plugin {
613
618
public static JarPlugin get = new JarPlugin ();
614
619
public JarPlugin () {
615
620
super ("org.apache.maven.plugins" , "maven-jar-plugin" , "3.2.0" );
616
- withBeforeGetConfiguration ( project -> {
621
+ onBeforeToXML (( project , pom ) -> {
617
622
addConfiguration ("archive manifest addClasspath" , "true" );
618
623
addConfiguration ("archive manifest mainClass" , project .mainClass );
619
624
addConfiguration ("finalName" , project .jarName .replace (".jar" , "" ));
@@ -628,7 +633,7 @@ public static class AssemblyPlugin extends Plugin {
628
633
public static AssemblyPlugin get = new AssemblyPlugin ();
629
634
public AssemblyPlugin () {
630
635
super ("org.apache.maven.plugins" , "maven-assembly-plugin" , "3.3.0" );
631
- withBeforeGetConfiguration ( project -> {
636
+ onBeforeToXML (( project , pom ) -> {
632
637
addConfiguration ("descriptorRefs descriptorRef" , "jar-with-dependencies" );
633
638
addConfiguration ("archive manifest mainClass" , project .mainClass );
634
639
addConfiguration ("finalName" , project .fatJarName .replace (".jar" , "" ));
@@ -648,7 +653,7 @@ public static class SourcePlugin extends Plugin {
648
653
public static SourcePlugin get = new SourcePlugin ();
649
654
public SourcePlugin () {
650
655
super ("org.apache.maven.plugins" , "maven-source-plugin" , "3.2.1" );
651
- withBeforeGetConfiguration ( project -> {
656
+ onBeforeToXML (( project , pom ) -> {
652
657
Execution execution = new Execution ("attach-sources" , null );
653
658
addExecution (execution );
654
659
execution .addGoal ("jar" );
@@ -663,7 +668,7 @@ public static class JavadocPlugin extends Plugin {
663
668
public static JavadocPlugin get = new JavadocPlugin ();
664
669
public JavadocPlugin () {
665
670
super ("org.apache.maven.plugins" , "maven-javadoc-plugin" , "3.0.0" );
666
- withBeforeGetConfiguration ( project -> {
671
+ onBeforeToXML (( project , pom ) -> {
667
672
Execution execution = new Execution ("resource-bundles" , "package" );
668
673
addExecution (execution );
669
674
execution .addGoal ("resource-bundle" );
@@ -681,7 +686,7 @@ public static class EnforcerPlugin extends Plugin {
681
686
public static EnforcerPlugin get = new EnforcerPlugin ();
682
687
public EnforcerPlugin () {
683
688
super ("org.apache.maven.plugins" , "maven-enforcer-plugin" , "3.3.0" );
684
- withBeforeGetConfiguration ( project -> {
689
+ onBeforeToXML (( project , pom ) -> {
685
690
Execution execution = new Execution ("enforce" , null );
686
691
addExecution (execution );
687
692
execution .addGoal ("enforce" );
0 commit comments