Search in sources :

Example 1 with PomModel

use of org.appformer.maven.support.PomModel in project drools by kiegroup.

the class AbstractKieModule method getPomModel.

public PomModel getPomModel() {
    if (pomModel == null) {
        try {
            byte[] pomXml = getPomXml();
            if (pomXml != null) {
                PomModel tempPomModel = PomModel.Parser.parse("pom.xml", new ByteArrayInputStream(pomXml));
                // throws an exception if invalid
                validatePomModel(tempPomModel);
                pomModel = tempPomModel;
            }
        } catch (Exception e) {
        // nothing to do as it was not possible to retrieve pom.xml
        }
    }
    return pomModel;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PomModel(org.appformer.maven.support.PomModel) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 2 with PomModel

use of org.appformer.maven.support.PomModel in project drools by kiegroup.

the class KieBuilderImpl method init.

private PomModel init() {
    KieServices ks = KieServices.Factory.get();
    results = new ResultsImpl();
    // if pomXML is null it will generate a default, using default ReleaseId
    // if pomXml is invalid, it assign pomModel to null
    PomModel pomModel = getPomModel();
    // if kModuleModelXML is null or invalid it will generate a default kModule, with a default kbase name
    buildKieModuleModel();
    if (pomModel != null) {
        // creates ReleaseId from build pom
        // If the pom was generated, it will be the same as teh default ReleaseId
        releaseId = pomModel.getReleaseId();
        // add all the pom dependencies to this builder ... not sure this is a good idea (?)
        KieRepositoryImpl repository = (KieRepositoryImpl) ks.getRepository();
        for (AFReleaseId dep : pomModel.getDependencies(DependencyFilter.COMPILE_FILTER)) {
            KieModule depModule = repository.getKieModule(adapt(dep), pomModel);
            if (depModule != null) {
                addKieDependency(depModule);
            }
        }
    } else {
        // if the pomModel is null it means that the provided pom.xml is invalid so use the default releaseId
        releaseId = KieServices.Factory.get().getRepository().getDefaultReleaseId();
    }
    return pomModel;
}
Also used : AFReleaseId(org.appformer.maven.support.AFReleaseId) PomModel(org.appformer.maven.support.PomModel) KieServices(org.kie.api.KieServices) KieModule(org.kie.api.builder.KieModule)

Example 3 with PomModel

use of org.appformer.maven.support.PomModel in project drools by kiegroup.

the class KieBuilderImpl method buildPomModel.

private PomModel buildPomModel() {
    pomXml = getOrGeneratePomXml(srcMfs);
    if (pomXml == null) {
        // will be null if the provided pom is invalid
        return null;
    }
    try {
        PomModel tempPomModel = PomModel.Parser.parse("pom.xml", new ByteArrayInputStream(pomXml));
        // throws an exception if invalid
        validatePomModel(tempPomModel);
        return tempPomModel;
    } catch (Exception e) {
        results.addMessage(Level.ERROR, "pom.xml", "maven pom.xml found, but unable to read\n" + e.getMessage());
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PomModel(org.appformer.maven.support.PomModel) IOException(java.io.IOException)

Example 4 with PomModel

use of org.appformer.maven.support.PomModel in project drools by kiegroup.

the class KieBuilderImpl method buildAll.

public KieBuilder buildAll(BiFunction<InternalKieModule, ClassLoader, KieModuleKieProject> kprojectSupplier, Predicate<String> classFilter) {
    PomModel pomModel = init();
    // kModuleModel will be null if a provided pom.xml or kmodule.xml is invalid
    if (!isBuilt() && kModuleModel != null) {
        trgMfs = new MemoryFileSystem();
        writePomAndKModule();
        addKBasesFilesToTrg();
        markSource();
        MemoryKieModule memoryKieModule = new MemoryKieModule(adapt(releaseId), kModuleModel, trgMfs);
        if (kieDependencies != null && !kieDependencies.isEmpty()) {
            for (KieModule kieModule : kieDependencies) {
                memoryKieModule.addKieDependency((InternalKieModule) kieModule);
            }
        }
        if (pomModel != null) {
            memoryKieModule.setPomModel(pomModel);
        }
        KieModuleKieProject kProject = kprojectSupplier.apply(memoryKieModule, classLoader);
        for (ReleaseId unresolvedDep : memoryKieModule.getUnresolvedDependencies()) {
            results.addMessage(Level.ERROR, "pom.xml", "Unresolved dependency " + unresolvedDep);
        }
        List<String> pmmlFileNames = getPmmlFileNames(srcMfs);
        if (pmmlFileNames != null && !pmmlFileNames.isEmpty()) {
            buildPMMLPojos(pmmlFileNames, kProject);
        }
        compileJavaClasses(kProject.getClassLoader(), classFilter);
        buildKieProject(results, kProject, trgMfs);
        kModule = kProject.getInternalKieModule();
    }
    return this;
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) PomModel(org.appformer.maven.support.PomModel) AFReleaseId(org.appformer.maven.support.AFReleaseId) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule)

Example 5 with PomModel

use of org.appformer.maven.support.PomModel in project drools by kiegroup.

the class KieBuilderTest method testSetPomModelReuse.

@Test
public void testSetPomModelReuse() throws IOException {
    String namespace = "org.kie.test";
    ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "pomModelReuse", "1.0-SNAPSHOT");
    String pom = KieBuilderImpl.generatePomXml(releaseId);
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    kfs.writePomXML(pom);
    // Create a KieBuilder instance
    KieBuilder kieBuilder1 = createKieBuilder(kfs);
    kieBuilder1.buildAll();
    // Get PomModel to re-use in second KieBuilder instance
    PomModel pomModel = ((KieBuilderImpl) kieBuilder1).getPomModel();
    kfs.writePomXML(pom);
    // Create another KieBuilder instance with the same KieFileSystem, setting PomModel
    KieBuilder kieBuilder2 = createKieBuilder(kfs);
    ((KieBuilderImpl) kieBuilder2).setPomModel(pomModel);
    kieBuilder2.buildAll();
    // Read pom.xml from first KieBuilder's KieModule
    InternalKieModule kieModule1 = (InternalKieModule) ((KieBuilderImpl) kieBuilder1).getKieModuleIgnoringErrors();
    final Reader reader1 = kieModule1.getResource("META-INF/maven/org.kie.test/pomModelReuse/pom.xml").getReader();
    int charCode;
    String readPom1 = "";
    while ((charCode = reader1.read()) != -1) {
        readPom1 = readPom1 + (char) charCode;
    }
    reader1.close();
    assertEquals(pom, readPom1);
    // Read pom.xml from second KieBuilder's KieModule
    InternalKieModule kieModule2 = (InternalKieModule) ((KieBuilderImpl) kieBuilder2).getKieModuleIgnoringErrors();
    final Reader reader2 = kieModule2.getResource("META-INF/maven/org.kie.test/pomModelReuse/pom.xml").getReader();
    String readPom2 = "";
    while ((charCode = reader2.read()) != -1) {
        readPom2 = readPom2 + (char) charCode;
    }
    reader1.close();
    assertEquals(pom, readPom2);
}
Also used : KieBuilderImpl(org.drools.compiler.kie.builder.impl.KieBuilderImpl) KieFileSystem(org.kie.api.builder.KieFileSystem) PomModel(org.appformer.maven.support.PomModel) Reader(java.io.Reader) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Aggregations

PomModel (org.appformer.maven.support.PomModel)7 IOException (java.io.IOException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 AFReleaseId (org.appformer.maven.support.AFReleaseId)2 Test (org.junit.Test)2 KieModule (org.kie.api.builder.KieModule)2 ReleaseId (org.kie.api.builder.ReleaseId)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 ZipFile (java.util.zip.ZipFile)1 MavenPomModelGenerator (org.appformer.maven.integration.MavenPomModelGenerator)1 PomModelGenerator (org.appformer.maven.support.PomModelGenerator)1 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)1 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)1 KieBuilderImpl (org.drools.compiler.kie.builder.impl.KieBuilderImpl)1 KieServices (org.kie.api.KieServices)1