use of io.fabric8.maven.generator.api.Generator in project fabric8-maven-plugin by fabric8io.
the class JavaExecGeneratorMainClassDeterminationTest method testMainClassDeterminationFromDetectionOnNonFatJar.
/**
* The main class is determined via main class detection in a non-fat-jar deployment
* @throws MojoExecutionException
*/
@Test
public void testMainClassDeterminationFromDetectionOnNonFatJar() throws MojoExecutionException {
new MockBuild();
new MockProcessorConfig(null);
new MockMavenProject();
new MockFatJarDetector(false);
new MockClassUtils();
final GeneratorContext generatorContext = new GeneratorContext.Builder().project(new MavenProject()).config(new ProcessorConfig()).strategy(OpenShiftBuildStrategy.docker).logger(log).build();
JavaExecGenerator generator = new JavaExecGenerator(generatorContext);
final List<ImageConfiguration> images = new ArrayList<ImageConfiguration>();
List<ImageConfiguration> customized = generator.customize(images, false);
assertEquals("1 images returned", (long) 1, (long) customized.size());
ImageConfiguration imageConfig = customized.get(0);
assertEquals("Image name", "TheImageName", imageConfig.getName());
assertEquals("Main Class set as environment variable", "the.detected.MainClass", imageConfig.getBuildConfiguration().getEnv().get(JavaExecGenerator.JAVA_MAIN_CLASS_ENV_VAR));
}
use of io.fabric8.maven.generator.api.Generator in project fabric8-maven-plugin by fabric8io.
the class JavaExecGeneratorMainClassDeterminationTest method testMainClassDeterminationFromFatJar.
/**
* The main class is determined as the Main-Class of a fat jar
* @throws MojoExecutionException
*/
@Test
public void testMainClassDeterminationFromFatJar() throws MojoExecutionException {
new MockBuild();
new MockProcessorConfig(null);
new MockMavenProject();
new MockFatJarDetector(true);
new MockJavaExecGenerator();
final GeneratorContext generatorContext = new GeneratorContext.Builder().project(new MavenProject()).config(new ProcessorConfig()).strategy(OpenShiftBuildStrategy.docker).logger(log).build();
JavaExecGenerator generator = new JavaExecGenerator(generatorContext);
final List<ImageConfiguration> images = new ArrayList<ImageConfiguration>();
List<ImageConfiguration> customized = generator.customize(images, false);
assertEquals("1 images returned", (long) 1, (long) customized.size());
ImageConfiguration imageConfig = customized.get(0);
assertEquals("Image name", "TheImageName", imageConfig.getName());
assertNull("Main Class is NOT set as environment variable#", imageConfig.getBuildConfiguration().getEnv().get(JavaExecGenerator.JAVA_MAIN_CLASS_ENV_VAR));
}
use of io.fabric8.maven.generator.api.Generator in project fabric8 by fabric8io.
the class KubernetesProviderProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
CompilationTaskFactory compilationTaskFactory = new CompilationTaskFactory(processingEnv);
Set<TypeElement> providers = new HashSet<>();
// 1st pass collect classes to compile.
for (Element element : roundEnv.getElementsAnnotatedWith(KubernetesProvider.class)) {
providers.add(getClassElement(element));
}
if (providers.isEmpty()) {
return true;
}
StringWriter writer = new StringWriter();
try {
Callable<Boolean> compileTask = compilationTaskFactory.create(providers, writer);
if (!compileTask.call()) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to compile provider classes. See output below.");
return false;
}
} catch (Exception e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error to compile provider classes, due to: " + e.getMessage() + ". See output below.");
return false;
} finally {
String output = writer.toString();
if (Strings.isNullOrBlank(output)) {
output = "success";
}
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Fabric8 model generator compiler output:" + output);
}
// 2nd pass generate json.
Map<String, Set> providedMap = new HashMap<>();
for (Element element : roundEnv.getElementsAnnotatedWith(KubernetesProvider.class)) {
try {
if (element instanceof ExecutableElement) {
Set provided = getProvidedSet(providedMap, element);
ExecutableElement methodElement = (ExecutableElement) element;
String methodName = methodElement.getSimpleName().toString();
TypeElement classElement = getClassElement(element);
Class<?> cls = Class.forName(classElement.getQualifiedName().toString());
Object instance = cls.newInstance();
Method providerMethod = instance.getClass().getDeclaredMethod(methodName);
if (providerMethod != null) {
providerMethod.setAccessible(true);
provided.add(providerMethod.invoke(instance));
}
}
} catch (Exception ex) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error creating Kubernetes configuration.");
}
}
for (Map.Entry<String, Set> entry : providedMap.entrySet()) {
KubernetesResource answer;
try {
answer = (KubernetesResource) KubernetesHelper.combineJson(entry.getValue().toArray());
generateKubernetesManifest(entry.getKey(), answer);
} catch (Exception e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to combine provider items");
return false;
}
}
return true;
}
Aggregations