From ba85552c8683f8538c791d5b61e624f4f0520f5c Mon Sep 17 00:00:00 2001 From: Osiris Team Date: Thu, 25 Jul 2024 19:15:51 +0200 Subject: [PATCH] 2.0.1 release - fix not all dependencies being added - enhanced template structure --- README.md | 48 ++++++------- pom.xml | 7 ++ src/main/java/JPM.java | 69 +++++++++++-------- src/main/java/com/mycompany/MyLib.java | 7 -- src/main/java/com/mycompany/lib2/MyLib2.java | 7 -- .../java/com/mycompany/MyMainClassTest.java | 9 +++ 6 files changed, 82 insertions(+), 65 deletions(-) delete mode 100644 src/main/java/com/mycompany/MyLib.java delete mode 100644 src/main/java/com/mycompany/lib2/MyLib2.java create mode 100644 src/test/java/com/mycompany/MyMainClassTest.java diff --git a/README.md b/README.md index 6434e4d..5e582c1 100644 --- a/README.md +++ b/README.md @@ -24,38 +24,38 @@ You can find a list here at [#1jpm-plugin](https://github.com/topics/1jpm-plugin ```java class ThisProject extends JPM.Project { - public ThisProject(List args) { - // Override default configurations - this.groupId = "com.mycompany"; - this.artifactId = "my-project"; - this.version = "1.0.0"; - this.mainClass = "com.mycompany.MyMainClass"; - this.jarName = "my-project.jar"; - this.fatJarName = "my-project-with-dependencies.jar"; + public ThisProject(List args) { + // Override default configurations + this.groupId = "com.mycompany"; + this.artifactId = "my-project"; + this.version = "1.0.0"; + this.mainClass = "com.mycompany.MyMainClass"; + this.jarName = "my-project.jar"; + this.fatJarName = "my-project-with-dependencies.jar"; - // Add some example dependencies - implementation("junit:junit:4.13.2"); - implementation("org.apache.commons:commons-lang3:3.12.0"); - // If there are duplicate dependencies with different versions force a specific version like so: - //forceImplementation("org.apache.commons:commons-lang3:3.12.0"); + // Add some example dependencies + testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.3"); + implementation("org.apache.commons:commons-lang3:3.12.0"); + // If there are duplicate dependencies with different versions force a specific version like so: + //forceImplementation("org.apache.commons:commons-lang3:3.12.0"); - // Add some compiler arguments - addCompilerArg("-Xlint:unchecked"); - addCompilerArg("-Xlint:deprecation"); - } + // Add some compiler arguments + addCompilerArg("-Xlint:unchecked"); + addCompilerArg("-Xlint:deprecation"); + } - public static void main(String[] args) throws Exception { - new ThisProject(Arrays.asList(args)).generatePom(); - JPM.executeMaven("clean", "package"); // or JPM.executeMaven(args); if you prefer the CLI, like "java JPM.java clean package" - } + public static void main(String[] args) throws Exception { + new ThisProject(Arrays.asList(args)).generatePom(); + JPM.executeMaven("clean", "package"); // or JPM.executeMaven(args); if you prefer the CLI, like "java JPM.java clean package" + } } class ThirdPartyPlugins extends JPM.Plugins{ - // Add third party plugins below, find them here: https://github.com/topics/1jpm-plugin?o=desc&s=updated - // (If you want to develop a plugin take a look at "JPM.Clean" class further below to get started) + // Add third party plugins below, find them here: https://github.com/topics/1jpm-plugin?o=desc&s=updated + // (If you want to develop a plugin take a look at "JPM.Clean" class further below to get started) } -// 1JPM version 2.0.0 by Osiris-Team +// 1JPM version 2.0.1 by Osiris-Team // To upgrade JPM, replace the JPM class below with its newer version public class JPM { //... diff --git a/pom.xml b/pom.xml index db20169..b566823 100644 --- a/pom.xml +++ b/pom.xml @@ -8,10 +8,17 @@ UTF-8 + + org.junit.jupiter + junit-jupiter-api + 5.10.3 + test + org.apache.commons commons-lang3 3.12.0 + compile diff --git a/src/main/java/JPM.java b/src/main/java/JPM.java index 6e9c815..5b921af 100644 --- a/src/main/java/JPM.java +++ b/src/main/java/JPM.java @@ -25,7 +25,7 @@ public ThisProject(List args) { this.fatJarName = "my-project-with-dependencies.jar"; // Add some example dependencies - implementation("junit:junit:4.13.2"); + testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.3"); implementation("org.apache.commons:commons-lang3:3.12.0"); // If there are duplicate dependencies with different versions force a specific version like so: //forceImplementation("org.apache.commons:commons-lang3:3.12.0"); @@ -46,7 +46,7 @@ class ThirdPartyPlugins extends JPM.Plugins{ // (If you want to develop a plugin take a look at "JPM.Clean" class further below to get started) } -// 1JPM version 2.0.0 by Osiris-Team +// 1JPM version 2.0.1 by Osiris-Team // To upgrade JPM, replace the JPM class below with its newer version public class JPM { public static final List plugins = new ArrayList<>(); @@ -156,6 +156,17 @@ public String toString() { return groupId + ":" + artifactId + ":" + version + ":" + scope; } + public XML toXML(){ + XML xml = new XML("dependency"); + xml.put("groupId", groupId); + xml.put("artifactId", artifactId); + xml.put("version", version); + if (scope != null) { + xml.put("scope", scope); + } + return xml; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -206,37 +217,40 @@ public XML(String rootName) { } // Method to append another XML object to this XML document's root. - public void append(XML otherXML) { + public XML add(XML otherXML) { Node importedNode = document.importNode(otherXML.root, true); root.appendChild(importedNode); + return this; } // Method to append another XML object to a specific element in this XML document. - public void put(String key, XML otherXML) { + public XML add(String key, XML otherXML) { Element parentElement = getOrCreateElement(key); Node importedNode = document.importNode(otherXML.root, true); parentElement.appendChild(importedNode); + return this; } // Method to add a value to the XML document at the specified path. - public Element put(String key, String value) { + public XML put(String key, String value) { Element currentElement = getOrCreateElement(key); if(value != null && !value.isEmpty()) currentElement.setTextContent(value); - return currentElement; + return this; } // Method to add a comment to the XML document at the specified path. - public void putComment(String key, String comment) { + public XML putComment(String key, String comment) { Element currentElement = getOrCreateElement(key); Node parentNode = currentElement.getParentNode(); Node commentNode = document.createComment(comment); // Insert the comment before the specified element. parentNode.insertBefore(commentNode, currentElement); + return this; } - public void putAttributes(String key, String... attributes) { + public XML putAttributes(String key, String... attributes) { if (attributes.length % 2 != 0) { throw new IllegalArgumentException("Attributes must be in key-value pairs."); } @@ -249,16 +263,18 @@ public void putAttributes(String key, String... attributes) { String attrValue = attributes[i + 1]; currentElement.setAttribute(attrName, attrValue); } + return this; } // Method to add attributes to an element in the XML document at the specified path. - public void putAttributes(String key, Map attributes) { + public XML putAttributes(String key, Map attributes) { Element currentElement = getOrCreateElement(key); // Set each attribute in the map on the element. for (Map.Entry entry : attributes.entrySet()) { currentElement.setAttribute(entry.getKey(), entry.getValue()); } + return this; } // Helper method to traverse or create elements based on a path. @@ -383,19 +399,14 @@ public XML getConfiguration(Project project) { // Add if not empty if (!executions.isEmpty()) { for (Execution execution : executions) { - xml.put("executions", execution.getConfiguration()); + xml.add("executions", execution.toXML()); } } // Add if not empty if (!dependencies.isEmpty()) { for (Dependency dependency : dependencies) { - xml.put("dependencies dependency groupId", dependency.groupId); - xml.put("dependencies dependency artifactId", dependency.artifactId); - xml.put("dependencies dependency version", dependency.version); - if (dependency.scope != null) { - xml.put("dependencies dependency scope", dependency.scope); - } + xml.add("dependencies", dependency.toXML()); } } @@ -425,7 +436,7 @@ public void addConfiguration(String key, String value) { configuration.put(key, value); } - public XML getConfiguration() { + public XML toXML() { // Create an instance of XML with the root element XML xml = new XML("execution"); @@ -477,14 +488,22 @@ public void addRepository(String url){ repositories.add(Repository.fromUrl(url)); } + public void testImplementation(String s){ + String[] split = s.split(":"); + if(split.length < 3) throw new RuntimeException("Does not contain all required details: "+s); + addDependency(split[0], split[1], split[2]).scope = "test"; + } + public void implementation(String s){ String[] split = s.split(":"); if(split.length < 3) throw new RuntimeException("Does not contain all required details: "+s); addDependency(split[0], split[1], split[2]); } - public void addDependency(String groupId, String artifactId, String version) { - dependencies.add(new Dependency(groupId, artifactId, version)); + public Dependency addDependency(String groupId, String artifactId, String version) { + Dependency dep = new Dependency(groupId, artifactId, version); + dependencies.add(dep); + return dep; } public void forceImplementation(String s){ @@ -533,27 +552,23 @@ public void generatePom() throws IOException { // Add if there are managed dependencies if (!dependenciesManaged.isEmpty()) { for (Dependency dep : dependenciesManaged) { - pom.put("dependencyManagement dependency groupId", dep.groupId); - pom.put("dependencyManagement dependency artifactId", dep.artifactId); - pom.put("dependencyManagement dependency version", dep.version); + pom.add("dependencyManagement", dep.toXML()); } } // Add if there are dependencies if (!dependencies.isEmpty()) { for (Dependency dep : dependencies) { - pom.put("dependencies dependency groupId", dep.groupId); - pom.put("dependencies dependency artifactId", dep.artifactId); - pom.put("dependencies dependency version", dep.version); + pom.add("dependencies", dep.toXML()); } } // Add section with plugins and resources for (Plugin plugin : JPM.plugins) { - pom.put("build plugins", plugin.getConfiguration(this)); + pom.add("build plugins", plugin.getConfiguration(this)); } for (Plugin plugin : plugins) { - pom.put("build plugins", plugin.getConfiguration(this)); + pom.add("build plugins", plugin.getConfiguration(this)); } // Add resources with a comment diff --git a/src/main/java/com/mycompany/MyLib.java b/src/main/java/com/mycompany/MyLib.java deleted file mode 100644 index f0e1a59..0000000 --- a/src/main/java/com/mycompany/MyLib.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.mycompany; - -public class MyLib { - public void hello(){ - System.out.println("Hello World from Lib!"); - } -} diff --git a/src/main/java/com/mycompany/lib2/MyLib2.java b/src/main/java/com/mycompany/lib2/MyLib2.java deleted file mode 100644 index ea93172..0000000 --- a/src/main/java/com/mycompany/lib2/MyLib2.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.mycompany.lib2; - -public class MyLib2 { - public void hello(){ - System.out.println("Hello World from Lib2!"); - } -} diff --git a/src/test/java/com/mycompany/MyMainClassTest.java b/src/test/java/com/mycompany/MyMainClassTest.java new file mode 100644 index 0000000..540b304 --- /dev/null +++ b/src/test/java/com/mycompany/MyMainClassTest.java @@ -0,0 +1,9 @@ +package com.mycompany; + +import static org.junit.jupiter.api.Assertions.*; +class MyMainClassTest { + + @org.junit.jupiter.api.Test + void main() { + } +} \ No newline at end of file