Search in sources :

Example 6 with PomModel

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

the class ClasspathKieProject method generatePomPropertiesFromPom.

private static String generatePomPropertiesFromPom(String rootPath) {
    // recurse until we reach root or find a pom.xml
    File file = null;
    for (File folder = new File(rootPath); folder.getParent() != null; folder = new File(folder.getParent())) {
        file = new File(folder, "pom.xml");
        if (file.exists()) {
            break;
        }
        file = null;
    }
    if (file != null) {
        try (FileInputStream fis = new FileInputStream(file)) {
            PomModel pomModel = PomModel.Parser.parse(rootPath + "/pom.xml", fis);
            // throws an exception if invalid
            KieBuilderImpl.validatePomModel(pomModel);
            ReleaseId gav = pomModel.getReleaseId();
            String str = KieBuilderImpl.generatePomProperties(gav);
            log.info("Recursed up folders, found and used pom.xml " + file);
            return str;
        } catch (Exception e) {
            log.error("As folder project tried to fall back to pom.xml " + file + "\nbut failed with exception:\n" + e.getMessage());
        }
    } else {
        log.warn("As folder project tried to fall back to pom.xml, but could not find one");
    }
    return null;
}
Also used : PomModel(org.kie.util.maven.support.PomModel) ReleaseId(org.kie.api.builder.ReleaseId) ZipFile(java.util.zip.ZipFile) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 7 with PomModel

use of org.kie.util.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");
    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.kie.util.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.kie.util.maven.support.PomModel)7 ReleaseId (org.kie.api.builder.ReleaseId)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 KieModule (org.kie.api.builder.KieModule)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 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 KieBuilder (org.kie.api.builder.KieBuilder)1 KieFileSystem (org.kie.api.builder.KieFileSystem)1 MavenPomModelGenerator (org.kie.maven.integration.MavenPomModelGenerator)1