Search in sources :

Example 1 with MULE_MAVEN_PLUGIN_ARTIFACT_ID

use of org.mule.runtime.module.artifact.api.classloader.MuleMavenPlugin.MULE_MAVEN_PLUGIN_ARTIFACT_ID in project mule by mulesoft.

the class DeployableMavenClassLoaderModelLoader method exportSharedLibrariesResourcesAndPackages.

private void exportSharedLibrariesResourcesAndPackages(File applicationFolder, ClassLoaderModelBuilder classLoaderModelBuilder, Set<BundleDependency> dependencies) {
    Model model = loadPomModel(applicationFolder);
    Build build = model.getBuild();
    if (build != null) {
        List<Plugin> plugins = build.getPlugins();
        if (plugins != null) {
            Optional<Plugin> packagingPluginOptional = plugins.stream().filter(plugin -> plugin.getArtifactId().equals(MULE_MAVEN_PLUGIN_ARTIFACT_ID) && plugin.getGroupId().equals(MULE_MAVEN_PLUGIN_GROUP_ID)).findFirst();
            packagingPluginOptional.ifPresent(packagingPlugin -> {
                Object configuration = packagingPlugin.getConfiguration();
                if (configuration != null) {
                    Xpp3Dom sharedLibrariesDom = ((Xpp3Dom) configuration).getChild("sharedLibraries");
                    if (sharedLibrariesDom != null) {
                        Xpp3Dom[] sharedLibraries = sharedLibrariesDom.getChildren("sharedLibrary");
                        if (sharedLibraries != null) {
                            FileJarExplorer fileJarExplorer = new FileJarExplorer();
                            for (Xpp3Dom sharedLibrary : sharedLibraries) {
                                String groupId = getSharedLibraryAttribute(applicationFolder, sharedLibrary, "groupId");
                                String artifactId = getSharedLibraryAttribute(applicationFolder, sharedLibrary, "artifactId");
                                Optional<BundleDependency> bundleDependencyOptional = dependencies.stream().filter(bundleDependency -> bundleDependency.getDescriptor().getArtifactId().equals(artifactId) && bundleDependency.getDescriptor().getGroupId().equals(groupId)).findFirst();
                                bundleDependencyOptional.map(bundleDependency -> {
                                    JarInfo jarInfo = fileJarExplorer.explore(bundleDependency.getBundleUri());
                                    classLoaderModelBuilder.exportingPackages(jarInfo.getPackages());
                                    classLoaderModelBuilder.exportingResources(jarInfo.getResources());
                                    return bundleDependency;
                                }).orElseThrow(() -> new MuleRuntimeException(I18nMessageFactory.createStaticMessage(format("Dependency %s:%s could not be found within the artifact %s. It must be declared within the maven dependencies of the artifact.", groupId, artifactId, applicationFolder.getName()))));
                            }
                        }
                    }
                }
            });
        }
    }
}
Also used : FileJarExplorer(org.mule.runtime.module.artifact.internal.util.FileJarExplorer) LoggerFactory(org.slf4j.LoggerFactory) BundleDescriptor(org.mule.runtime.module.artifact.api.descriptor.BundleDescriptor) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Build(org.apache.maven.model.Build) MULE_MAVEN_PLUGIN_ARTIFACT_ID(org.mule.runtime.module.artifact.api.classloader.MuleMavenPlugin.MULE_MAVEN_PLUGIN_ARTIFACT_ID) ClassLoaderModel(org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModel) Map(java.util.Map) Preconditions.checkState(org.mule.runtime.api.util.Preconditions.checkState) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) DOMAIN(org.mule.runtime.core.api.config.bootstrap.ArtifactType.DOMAIN) POLICY(org.mule.runtime.core.api.config.bootstrap.ArtifactType.POLICY) MULE_LOADER_ID(org.mule.runtime.deployment.model.api.artifact.ArtifactDescriptorConstants.MULE_LOADER_ID) MavenClient(org.mule.maven.client.api.MavenClient) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) INCLUDE_TEST_DEPENDENCIES(org.mule.runtime.deployment.model.api.artifact.ArtifactDescriptorConstants.INCLUDE_TEST_DEPENDENCIES) BundleDependency(org.mule.runtime.module.artifact.api.descriptor.BundleDependency) AbstractMavenClassLoaderModelLoader(org.mule.runtime.module.deployment.impl.internal.maven.AbstractMavenClassLoaderModelLoader) Set(java.util.Set) LocalRepositorySupplierFactory(org.mule.maven.client.api.LocalRepositorySupplierFactory) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) String.format(java.lang.String.format) File(java.io.File) I18nMessageFactory(org.mule.runtime.api.i18n.I18nMessageFactory) ArtifactType(org.mule.runtime.core.api.config.bootstrap.ArtifactType) List(java.util.List) Plugin(org.apache.maven.model.Plugin) APP(org.mule.runtime.core.api.config.bootstrap.ArtifactType.APP) MULE_MAVEN_PLUGIN_GROUP_ID(org.mule.runtime.module.artifact.api.classloader.MuleMavenPlugin.MULE_MAVEN_PLUGIN_GROUP_ID) MavenUtils.getPomModelFolder(org.mule.runtime.module.deployment.impl.internal.maven.MavenUtils.getPomModelFolder) Optional(java.util.Optional) ClassLoaderModelBuilder(org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModel.ClassLoaderModelBuilder) JarInfo(org.mule.runtime.module.artifact.internal.util.JarInfo) Model(org.apache.maven.model.Model) FileJarExplorer(org.mule.runtime.module.artifact.internal.util.FileJarExplorer) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) BundleDependency(org.mule.runtime.module.artifact.api.descriptor.BundleDependency) JarInfo(org.mule.runtime.module.artifact.internal.util.JarInfo) Build(org.apache.maven.model.Build) ClassLoaderModel(org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModel) Model(org.apache.maven.model.Model) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Plugin(org.apache.maven.model.Plugin)

Example 2 with MULE_MAVEN_PLUGIN_ARTIFACT_ID

use of org.mule.runtime.module.artifact.api.classloader.MuleMavenPlugin.MULE_MAVEN_PLUGIN_ARTIFACT_ID in project mule by mulesoft.

the class MavenUtils method addSharedLibraryDependency.

/**
 * Adds a shared library to the pom model. If the plugin does not exists yet in the model then it will create it.
 *
 * @param model the pom model
 * @param dependency the descriptor of the dependency
 */
public static void addSharedLibraryDependency(Model model, Dependency dependency) {
    Build build = model.getBuild();
    if (build == null) {
        build = new Build();
        model.setBuild(build);
    }
    List<Plugin> plugins = build.getPlugins();
    if (plugins == null) {
        plugins = new ArrayList<>();
        build.setPlugins(plugins);
    }
    Optional<Plugin> pluginOptional = plugins.stream().filter(plugin -> plugin.getGroupId().equals(MULE_MAVEN_PLUGIN_GROUP_ID) && plugin.getArtifactId().equals(MULE_MAVEN_PLUGIN_ARTIFACT_ID)).findFirst();
    List<Plugin> finalPlugins = plugins;
    Plugin plugin = pluginOptional.orElseGet(() -> {
        Plugin muleMavenPlugin = new Plugin();
        muleMavenPlugin.setGroupId(MULE_MAVEN_PLUGIN_GROUP_ID);
        muleMavenPlugin.setArtifactId(MULE_MAVEN_PLUGIN_ARTIFACT_ID);
        finalPlugins.add(muleMavenPlugin);
        return muleMavenPlugin;
    });
    Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
    if (configuration == null) {
        configuration = new Xpp3Dom("configuration");
        plugin.setConfiguration(configuration);
    }
    Xpp3Dom sharedLibrariesDom = configuration.getChild(SHARED_LIBRARIES_ELEMENT);
    if (sharedLibrariesDom == null) {
        sharedLibrariesDom = new Xpp3Dom(SHARED_LIBRARIES_ELEMENT);
        configuration.addChild(sharedLibrariesDom);
    }
    Xpp3Dom sharedLibraryDom = new Xpp3Dom("sharedLibrary");
    sharedLibrariesDom.addChild(sharedLibraryDom);
    Xpp3Dom groupIdDom = new Xpp3Dom("groupId");
    groupIdDom.setValue(dependency.getGroupId());
    sharedLibraryDom.addChild(groupIdDom);
    Xpp3Dom artifactIdDom = new Xpp3Dom("artifactId");
    artifactIdDom.setValue(dependency.getArtifactId());
    sharedLibraryDom.addChild(artifactIdDom);
}
Also used : META_INF(org.mule.runtime.deployment.model.api.policy.PolicyTemplateDescriptor.META_INF) FileUtils.createFile(org.mule.runtime.core.api.util.FileUtils.createFile) URL(java.net.URL) JarUtils.getUrlWithinJar(org.mule.runtime.core.internal.util.JarUtils.getUrlWithinJar) ArtifactDescriptorCreateException(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptorCreateException) Dependency(org.apache.maven.model.Dependency) MULE_ARTIFACT_PATH_INSIDE_JAR(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor.MULE_ARTIFACT_PATH_INSIDE_JAR) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Build(org.apache.maven.model.Build) MULE_MAVEN_PLUGIN_ARTIFACT_ID(org.mule.runtime.module.artifact.api.classloader.MuleMavenPlugin.MULE_MAVEN_PLUGIN_ARTIFACT_ID) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) JarUtils.loadFileContentFrom(org.mule.runtime.core.internal.util.JarUtils.loadFileContentFrom) MULE_PLUGIN_POM(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor.MULE_PLUGIN_POM) File.separator(java.io.File.separator) Preconditions.checkState(org.mule.runtime.api.util.Preconditions.checkState) JarUtils.getUrlsWithinJar(org.mule.runtime.core.internal.util.JarUtils.getUrlsWithinJar) DIRECTORY(org.apache.commons.io.filefilter.DirectoryFileFilter.DIRECTORY) ArtifactPluginDescriptor(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor) FileWriter(java.io.FileWriter) FileOutputStream(java.io.FileOutputStream) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) IOException(java.io.IOException) String.format(java.lang.String.format) File(java.io.File) List(java.util.List) Plugin(org.apache.maven.model.Plugin) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer) FileFilter(java.io.FileFilter) Paths(java.nio.file.Paths) MULE_MAVEN_PLUGIN_GROUP_ID(org.mule.runtime.module.artifact.api.classloader.MuleMavenPlugin.MULE_MAVEN_PLUGIN_GROUP_ID) Optional(java.util.Optional) FileReader(java.io.FileReader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) InputStream(java.io.InputStream) Model(org.apache.maven.model.Model) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Build(org.apache.maven.model.Build) Plugin(org.apache.maven.model.Plugin)

Aggregations

File (java.io.File)2 String.format (java.lang.String.format)2 List (java.util.List)2 Optional (java.util.Optional)2 Build (org.apache.maven.model.Build)2 Model (org.apache.maven.model.Model)2 Plugin (org.apache.maven.model.Plugin)2 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)2 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)2 Preconditions.checkState (org.mule.runtime.api.util.Preconditions.checkState)2 MULE_MAVEN_PLUGIN_ARTIFACT_ID (org.mule.runtime.module.artifact.api.classloader.MuleMavenPlugin.MULE_MAVEN_PLUGIN_ARTIFACT_ID)2 MULE_MAVEN_PLUGIN_GROUP_ID (org.mule.runtime.module.artifact.api.classloader.MuleMavenPlugin.MULE_MAVEN_PLUGIN_GROUP_ID)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File.separator (java.io.File.separator)1 FileFilter (java.io.FileFilter)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1