Skip to content

Commit

Permalink
3.3.0
Browse files Browse the repository at this point in the history
- Introduction of the $("key"); function to easily retrieve values for props defined in the nearest JPM.properties file
- Self-updating
- remove some warnings
  • Loading branch information
Osiris-Team committed Sep 1, 2024
1 parent c6296e4 commit eb6aa43
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 18 deletions.
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# 1JPM
1 Java Project Manager (1JPM), is a Maven/Gradle alternative with a twist.
It's a single Java file itself, which should be edited by you to configure your project.

Meaning instead of writing XML or Groovy/DSL, your build file is Java code too.
**To build your project, [download/copy the JPM.java file](https://github.com/Osiris-Team/1JPM/releases/) into your project, open a terminal and execute:**

- Java 11 and above: `java JPM.java`
- Java 8 to 10: `javac JPM.java && java -cp . JPM`
**To build your project, simply [drag-and-drop the JPM.java file](https://github.com/Osiris-Team/1JPM/releases/)
into your project, open a terminal and execute `java JPM.java` (Java 11 and above).**

<details>
<summary>Java 8 to 10 and JDK notes</summary>

- Execute: `javac JPM.java && java -cp . JPM`
- Earlier Java versions are not supported
- Make sure you use a
[globally installed JDK](https://adoptium.net/temurin/releases/?os=windows&package=jdk) (not JRE)
with JAVA_HOME set
- Make sure you use a [globally installed JDK](https://adoptium.net/temurin/releases/?os=windows&package=jdk)
(not JRE) with JAVA_HOME set
</details>


Good to know:
- This repository functions as a template too
- 1JPM is Maven based, thus great IDE support by default
- 1JPM includes some extra plugins to increase runtime safety and provide additional features out of the box

Below you can see the example configuration which runs the `clean package` tasks.
This compiles and creates a jar file from your code, and additionally creates the sources,
javadoc and with-dependencies jars.

```java
public class JPM {
public static class ThisProject extends JPM.Project {
Expand Down Expand Up @@ -72,6 +72,10 @@ public class JPM {
}
```

Above you can see the example configuration which runs the `clean package` tasks.
This compiles and creates a jar file from your code, and additionally creates the sources,
javadoc and with-dependencies jars.

### Additional goodies

#### 1JPM automatically resolves parent and child projects
Expand Down Expand Up @@ -179,6 +183,8 @@ How many lines of relevant build code do we save compared to Maven?
Thus we write the same config with **263 lines less** code (which is a **3x** saving) when using 1JPM!
</details>

#### 1JPM is able to auto-update itself


## Why a single file?

Expand Down
60 changes: 53 additions & 7 deletions src/main/java/com/mycompany/myproject/JPM.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -70,7 +72,8 @@ public static class ThirdPartyPlugins extends JPM.Plugins{
}

// 1JPM version 3.3.0 by Osiris-Team: https://github.com/Osiris-Team/1JPM
// To upgrade JPM, replace everything below with its newer version
// Do not edit anything below, since changes will be lost due to auto-updating.
// You can also do this manually, by replacing everything below with its newer version and updating the imports.
public static final List<Plugin> plugins = new ArrayList<>();
public static final String mavenVersion = "3.9.8";
public static final String mavenWrapperVersion = "3.3.2";
Expand Down Expand Up @@ -141,7 +144,7 @@ public static void downloadMavenWrapper(File script) throws IOException {
String wrapperUrl = mavenWrapperScriptUrlBase + script.getName();

System.out.println("Downloading file from: " + wrapperUrl);
URL url = new URL(wrapperUrl);
URL url = toUrl(wrapperUrl);
script.getParentFile().mkdirs();
Files.copy(url.openStream(), script.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
Expand All @@ -150,7 +153,7 @@ public static void downloadMavenWrapperJar(File jar) throws IOException {
String wrapperUrl = mavenWrapperJarUrl;

System.out.println("Downloading file from: " + wrapperUrl);
URL url = new URL(wrapperUrl);
URL url = toUrl(wrapperUrl);
jar.getParentFile().mkdirs();
Files.copy(url.openStream(), jar.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
Expand Down Expand Up @@ -224,8 +227,26 @@ public static void createMavenWrapperProperties(File propertiesFile) throws IOEx
public static void updateSelfIfNeeded() {
try{
System.out.println("Downloading file from: " + jpmLatestUrl);
File jpmFile = new File(System.getProperty("user.dir") + "/JPM.java");
URL url = new URL(jpmLatestUrl);
File cwd = new File(System.getProperty("user.dir"));
File jpmFile = new File(cwd + "/JPM.java");

if (!jpmFile.exists()) {
jpmFile = new File(cwd + "/src/main/java/JPM.java");
if (!jpmFile.exists()) {
String jpmPackagePath = "/"+JPM.class.getPackage().getName().replace(".", "/");
jpmFile = new File(cwd+"/src/main/java"+jpmPackagePath+"/JPM.java");

// If still not found, search all subdirs by shallowest first
if (!jpmFile.exists()) {
jpmFile = searchForFileBreadthFirst(cwd, "JPM.java");
if (jpmFile == null) {
throw new FileNotFoundException("JPM.java file not found in the project directory.");
}
}
}
}

URL url = toUrl(jpmLatestUrl);
jpmFile.getParentFile().mkdirs();

String jpmJavaContent = contentToString(url);
Expand Down Expand Up @@ -262,6 +283,27 @@ public static void updateSelfIfNeeded() {
}
}

private static File searchForFileBreadthFirst(File rootDir, String fileName) {
Queue<File> directories = new LinkedList<>();
directories.add(rootDir);

while (!directories.isEmpty()) {
File dir = directories.poll();
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
directories.add(file); // Add directories to the queue for further searching
} else if (file.getName().equals(fileName)) {
return file; // Return the file if found
}
}
}
}
return null; // File not found
}


private static String extractJPMVersion(String content) {
Pattern pattern = Pattern.compile("//\\s*1JPM\\s*version\\s*(\\d+\\.\\d+\\.\\d+)\\s*by\\s*Osiris-Team:");
Matcher matcher = pattern.matcher(content);
Expand Down Expand Up @@ -335,6 +377,10 @@ private static String mergeJPMImports(String currentContent, String latestConten
return mergedImports.toString();
}

private static URL toUrl(String url) throws MalformedURLException {
return URL.of(URI.create(url), null);
}


/**
* This is going to download and copy the latest JPM.java file into all child projects it can find in this directory,
Expand Down Expand Up @@ -370,7 +416,7 @@ public static void portChildProjects() throws Exception {
continue;
}
System.out.println("Downloading file from: " + jpmLatestUrl);
URL url = new URL(jpmLatestUrl);
URL url = toUrl(jpmLatestUrl);
jpmFile.getParentFile().mkdirs();
String jpmJavaContent = contentToString(url);
jpmJavaContent = jpmJavaContent.replace(".myproject", "."+childProjectDir.getName())
Expand Down Expand Up @@ -498,7 +544,7 @@ private static void suggestVersions(Project project, String groupId, String arti
for (Repository _repo : project.repositories) {
String repo = _repo.url;
try {
URL url = new URL(repo + "/" + groupId.replace('.', '/') + "/" + artifactId + "/maven-metadata.xml");
URL url = toUrl(repo + "/" + groupId.replace('.', '/') + "/" + artifactId + "/maven-metadata.xml");
System.out.println("Checking repository: " + url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
Expand Down

0 comments on commit eb6aa43

Please sign in to comment.