Search in sources :

Example 21 with Generator

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));
}
Also used : ArrayList(java.util.ArrayList) GeneratorContext(io.fabric8.maven.generator.api.GeneratorContext) ProcessorConfig(io.fabric8.maven.core.config.ProcessorConfig) MavenProject(org.apache.maven.project.MavenProject) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) Test(org.junit.Test)

Example 22 with Generator

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));
}
Also used : ArrayList(java.util.ArrayList) GeneratorContext(io.fabric8.maven.generator.api.GeneratorContext) ProcessorConfig(io.fabric8.maven.core.config.ProcessorConfig) MavenProject(org.apache.maven.project.MavenProject) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) Test(org.junit.Test)

Example 23 with Generator

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;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Method(java.lang.reflect.Method) StringWriter(java.io.StringWriter) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource)

Aggregations

Test (org.junit.Test)9 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)7 GeneratorContext (io.fabric8.maven.generator.api.GeneratorContext)7 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)6 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)5 Properties (java.util.Properties)5 KubernetesModelGenerator (com.github.isdream.chameleon.container.kubernetes.KubernetesModelGenerator)4 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)4 ArrayList (java.util.ArrayList)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 MavenProject (org.apache.maven.project.MavenProject)3 KubernetesModelGenerator (com.github.isdream.jointware.kubernetes.KubernetesModelGenerator)2 KubernetesResource (io.fabric8.kubernetes.api.model.KubernetesResource)2 StringWriter (java.io.StringWriter)2 Method (java.lang.reflect.Method)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 Visitable (io.fabric8.kubernetes.api.builder.Visitable)1 Visitor (io.fabric8.kubernetes.api.builder.Visitor)1 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)1 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)1