-
Notifications
You must be signed in to change notification settings - Fork 223
Pure Java Project Generator #2083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
9dab736
feat: project generator maven plugin
csviri 3625f25
files to add
csviri 6341e7f
progress
csviri c9fbe5f
generated project compiles
csviri c61e497
progress
csviri 87afba3
improvements
csviri 71abc9d
add plugin plugin
csviri 5217ac1
log4j config & other improvements
csviri 3900d23
testing if generated project compiles
csviri f0e5c0b
flag
csviri 572d719
logs
csviri c5413ae
flag
csviri f7eb814
list poms
csviri 07400a6
running fix?
csviri 93d1166
cleanup
csviri 823d2c7
improve
csviri 20d0912
remove maven wrapper
csviri 7959918
chnages from CR
csviri a67cd3e
fix pom
csviri 828a703
compilation fix
csviri ce93763
empty status
csviri 764bdff
changes requested in PR
csviri 9bdc051
readme
csviri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<artifactId>java-operator-sdk</artifactId> | ||
<groupId>io.javaoperatorsdk</groupId> | ||
<version>4.5.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>bootstrapper</artifactId> | ||
<packaging>maven-plugin</packaging> | ||
<name>Operator SDK - Bootstrapper Maven Plugin</name> | ||
<description>Operator SDK - Bootstrapper Maven Plugin</description> | ||
|
||
<properties> | ||
<maven-plugin-annotations.version>3.9.0</maven-plugin-annotations.version> | ||
<maven-plugin-api.version>3.9.5</maven-plugin-api.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.maven</groupId> | ||
<artifactId>maven-plugin-api</artifactId> | ||
<version>${maven-plugin-api.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.maven.plugin-tools</groupId> | ||
<artifactId>maven-plugin-annotations</artifactId> | ||
<version>${maven-plugin-annotations.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-slf4j-impl</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-io</groupId> | ||
<artifactId>commons-io</artifactId> | ||
<version>2.14.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.spullara.mustache.java</groupId> | ||
<artifactId>compiler</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-plugin-plugin</artifactId> | ||
<version>3.9.0</version> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>templating-maven-plugin</artifactId> | ||
<version>1.0.0</version> | ||
<executions> | ||
<execution> | ||
<id>filtering-java-templates</id> | ||
<goals> | ||
<goal>filter-sources</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
|
||
</plugins> | ||
</build> | ||
</project> |
10 changes: 10 additions & 0 deletions
10
...ven-plugin/src/main/java-templates/io/javaoperatorsdk/operator/bootstrapper/Versions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.javaoperatorsdk.bootstrapper; | ||
|
||
public final class Versions { | ||
|
||
private Versions() {} | ||
|
||
public static final String JOSDK = "${project.version}"; | ||
public static final String KUBERNETES_CLIENT = "${fabric8-client.version}"; | ||
|
||
} |
134 changes: 134 additions & 0 deletions
134
bootstrapper-maven-plugin/src/main/java/io/javaoperatorsdk/boostrapper/Bootstrapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package io.javaoperatorsdk.boostrapper; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.javaoperatorsdk.bootstrapper.Versions; | ||
|
||
import com.github.mustachejava.DefaultMustacheFactory; | ||
import com.github.mustachejava.MustacheFactory; | ||
|
||
public class Bootstrapper { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(Bootstrapper.class); | ||
|
||
private MustacheFactory mustacheFactory = new DefaultMustacheFactory(); | ||
|
||
private static final List<String> TOP_LEVEL_STATIC_FILES = | ||
List.of(".gitignore", "README.md"); | ||
private static final List<String> JAVA_FILES = | ||
List.of("CustomResource.java", "Reconciler.java", | ||
"Spec.java", "Status.java"); | ||
|
||
public void create(File targetDir, String groupId, String artifactId) { | ||
try { | ||
log.info("Generating project to: {}", targetDir.getPath()); | ||
var projectDir = new File(targetDir, artifactId); | ||
FileUtils.forceMkdir(projectDir); | ||
addStaticFiles(projectDir); | ||
addTemplatedFiles(projectDir, groupId, artifactId); | ||
addJavaFiles(projectDir, groupId, artifactId); | ||
addResourceFiles(projectDir, groupId, artifactId); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void addResourceFiles(File projectDir, String groupId, String artifactId) { | ||
try { | ||
var target = new File(projectDir, "src/main/resources"); | ||
FileUtils.forceMkdir(target); | ||
addTemplatedFile(target, "log4j2.xml", groupId, artifactId, target, null); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void addJavaFiles(File projectDir, String groupId, String artifactId) { | ||
try { | ||
var packages = groupId.replace(".", File.separator); | ||
var targetDir = new File(projectDir, "src/main/java/" + packages); | ||
FileUtils.forceMkdir(targetDir); | ||
var classFileNamePrefix = artifactClassId(artifactId); | ||
JAVA_FILES.forEach(f -> addTemplatedFile(projectDir, f, groupId, artifactId, targetDir, | ||
classFileNamePrefix + f)); | ||
|
||
addTemplatedFile(projectDir, "Runner.java", groupId, artifactId, targetDir, null); | ||
addTemplatedFile(projectDir, "ConfigMapDependentResource.java", groupId, artifactId, | ||
targetDir, null); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
} | ||
|
||
private void addTemplatedFiles(File projectDir, String groupId, String artifactId) { | ||
addTemplatedFile(projectDir, "pom.xml", groupId, artifactId); | ||
addTemplatedFile(projectDir, "k8s/test-resource.yaml", groupId, artifactId); | ||
} | ||
|
||
private void addTemplatedFile(File projectDir, String fileName, String groupId, | ||
String artifactId) { | ||
addTemplatedFile(projectDir, fileName, groupId, artifactId, null, null); | ||
} | ||
|
||
private void addTemplatedFile(File projectDir, String fileName, String groupId, String artifactId, | ||
File targetDir, String targetFileName) { | ||
try { | ||
var values = Map.of("groupId", groupId, "artifactId", artifactId, | ||
"artifactClassId", artifactClassId(artifactId), | ||
"josdkVersion", Versions.JOSDK, | ||
"fabric8Version", Versions.KUBERNETES_CLIENT); | ||
|
||
var mustache = mustacheFactory.compile("templates/" + fileName); | ||
var targetFile = new File(targetDir == null ? projectDir : targetDir, | ||
targetFileName == null ? fileName : targetFileName); | ||
FileUtils.forceMkdir(targetFile.getParentFile()); | ||
var writer = new FileWriter(targetFile); | ||
mustache.execute(writer, values); | ||
writer.flush(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void addStaticFiles(File projectDir) { | ||
TOP_LEVEL_STATIC_FILES.forEach(f -> addStaticFile(projectDir, f)); | ||
} | ||
|
||
private void addStaticFile(File targetDir, String fileName) { | ||
addStaticFile(targetDir, fileName, null); | ||
} | ||
|
||
private void addStaticFile(File targetDir, String fileName, String subDir) { | ||
String sourcePath = subDir == null ? "/static/" : "/static/" + subDir; | ||
String path = sourcePath + fileName; | ||
try (var is = Bootstrapper.class.getResourceAsStream(path)) { | ||
targetDir = subDir == null ? targetDir : new File(targetDir, subDir); | ||
if (subDir != null) { | ||
FileUtils.forceMkdir(targetDir); | ||
} | ||
FileUtils.copyInputStreamToFile(is, new File(targetDir, fileName)); | ||
} catch (IOException e) { | ||
throw new RuntimeException("File path: " + path, e); | ||
} | ||
|
||
} | ||
|
||
public static String artifactClassId(String artifactId) { | ||
var parts = artifactId.split("-"); | ||
return Arrays.stream(parts).map(p -> p.substring(0, 1) | ||
.toUpperCase() + p.substring(1)) | ||
.collect(Collectors.joining("")); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
bootstrapper-maven-plugin/src/main/java/io/javaoperatorsdk/boostrapper/BootstrapperMojo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.javaoperatorsdk.boostrapper; | ||
|
||
import java.io.File; | ||
|
||
import org.apache.maven.plugin.AbstractMojo; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
|
||
@Mojo(name = "create", requiresProject = false) | ||
public class BootstrapperMojo | ||
extends AbstractMojo { | ||
|
||
@Parameter(defaultValue = "${projectGroupId}") | ||
protected String projectGroupId; | ||
|
||
@Parameter(defaultValue = "${projectArtifactId}") | ||
protected String projectArtifactId; | ||
|
||
public void execute() | ||
throws MojoExecutionException { | ||
String userDir = System.getProperty("user.dir"); | ||
new Bootstrapper().create(new File(userDir), projectGroupId, projectArtifactId); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration name="TestConfig" status="WARN"> | ||
<Appenders> | ||
<Console name="Console" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="%d %threadId %-30c{1.} [%-5level] %msg%n%throwable"/> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<Logger level="debug" name="io.javaoperatorsdk" additivity="false"> | ||
<AppenderRef ref="Console"/> | ||
</Logger> | ||
<Root level="info"> | ||
<AppenderRef ref="Console"/> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
41 changes: 41 additions & 0 deletions
41
bootstrapper-maven-plugin/src/main/resources/static/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#Maven | ||
target/ | ||
pom.xml.tag | ||
pom.xml.releaseBackup | ||
pom.xml.versionsBackup | ||
release.properties | ||
.flattened-pom.xml | ||
|
||
# Eclipse | ||
.project | ||
.classpath | ||
.settings/ | ||
bin/ | ||
|
||
# IntelliJ | ||
.idea | ||
*.ipr | ||
*.iml | ||
*.iws | ||
|
||
# NetBeans | ||
nb-configuration.xml | ||
|
||
# Visual Studio Code | ||
.vscode | ||
.factorypath | ||
|
||
# OSX | ||
.DS_Store | ||
|
||
# Vim | ||
*.swp | ||
*.swo | ||
|
||
# patch | ||
*.orig | ||
*.rej | ||
|
||
# Local environment | ||
.env | ||
|
3 changes: 3 additions & 0 deletions
3
bootstrapper-maven-plugin/src/main/resources/static/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Generated Project Skeleton | ||
|
||
A simple operator that copies the value in a spec to a ConfigMap. |
34 changes: 34 additions & 0 deletions
34
bootstrapper-maven-plugin/src/main/resources/templates/ConfigMapDependentResource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package {{groupId}}; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ConfigMapBuilder; | ||
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource; | ||
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent; | ||
import {{groupId}}.{{artifactClassId}}CustomResource; | ||
|
||
@KubernetesDependent | ||
public class ConfigMapDependentResource | ||
extends CRUDKubernetesDependentResource<ConfigMap, {{artifactClassId}}CustomResource> { | ||
|
||
public ConfigMapDependentResource() { | ||
super(ConfigMap.class); | ||
} | ||
|
||
@Override | ||
protected ConfigMap desired({{artifactClassId}}CustomResource primary, | ||
Context<{{artifactClassId}}CustomResource> context) { | ||
return new ConfigMapBuilder() | ||
.withMetadata( | ||
new ObjectMetaBuilder() | ||
.withName(primary.getMetadata().getName()) | ||
.withNamespace(primary.getMetadata().getNamespace()) | ||
.build()) | ||
.withData(Map.of("data", primary.getSpec().getValue())) | ||
.build(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
bootstrapper-maven-plugin/src/main/resources/templates/CustomResource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package {{groupId}}; | ||
|
||
import io.fabric8.kubernetes.client.CustomResource; | ||
import io.fabric8.kubernetes.api.model.Namespaced; | ||
import io.fabric8.kubernetes.model.annotation.Group; | ||
import io.fabric8.kubernetes.model.annotation.Version; | ||
|
||
@Group("{{groupId}}") | ||
@Version("v1") | ||
csviri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class {{artifactClassId}}CustomResource extends CustomResource<{{artifactClassId}}Spec,{{artifactClassId}}Status> implements Namespaced { | ||
csviri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this changed to to an install instead of simply testing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bom was then not accessible from the project, if there is no install