Skip to content

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 23 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
./mvnw ${MAVEN_ARGS} formatter:validate -Dconfigfile=$PWD/contributing/eclipse-google-style.xml -pl '!operator-framework-bom' --file pom.xml
./mvnw ${MAVEN_ARGS} impsort:check -pl '!operator-framework-bom' --file pom.xml
- name: Run unit tests
run: ./mvnw ${MAVEN_ARGS} -B test --file pom.xml
run: ./mvnw ${MAVEN_ARGS} clean install --file pom.xml
Copy link
Collaborator

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?

Copy link
Collaborator Author

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


integration_tests:
strategy:
Expand Down
95 changes: 95 additions & 0 deletions bootstrapper-maven-plugin/pom.xml
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>
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}";

}
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(""));
}

}
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);
}
}
16 changes: 16 additions & 0 deletions bootstrapper-maven-plugin/src/main/resources/log4j2.xml
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 bootstrapper-maven-plugin/src/main/resources/static/.gitignore
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 bootstrapper-maven-plugin/src/main/resources/static/README.md
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.
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();
}
}
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")
public class {{artifactClassId}}CustomResource extends CustomResource<{{artifactClassId}}Spec,{{artifactClassId}}Status> implements Namespaced {
}
Loading