Search in sources :

Example 1 with MavenRunnerSettings

use of org.jetbrains.idea.maven.execution.MavenRunnerSettings in project intellij-community by JetBrains.

the class MavenPropertyPsiReference method collectVariants.

protected void collectVariants(final List<Object> result, Set<String> variants) {
    int prefixLength = 0;
    if (myText.startsWith("pom.")) {
        prefixLength = "pom.".length();
    } else if (myText.startsWith("project.")) {
        prefixLength = "project.".length();
    }
    MavenProject mavenProject = myMavenProject;
    while (myText.startsWith("parent.", prefixLength)) {
        MavenId parentId = mavenProject.getParentId();
        if (parentId == null)
            return;
        mavenProject = myProjectsManager.findProject(parentId);
        if (mavenProject == null)
            return;
        prefixLength += "parent.".length();
    }
    final String prefix = prefixLength == 0 ? null : myText.substring(0, prefixLength);
    PsiDirectory baseDir = getBaseDir(mavenProject);
    addVariant(result, "basedir", baseDir, prefix, MavenIcons.MavenLogo);
    if (prefix == null) {
        result.add(createLookupElement(baseDir, "project.baseUri", MavenIcons.MavenLogo));
        result.add(createLookupElement(baseDir, "pom.baseUri", MavenIcons.MavenLogo));
        result.add(LookupElementBuilder.create(TIMESTAMP_PROP).withIcon(MavenIcons.MavenLogo));
    }
    processSchema(MavenSchemaProvider.MAVEN_PROJECT_SCHEMA_URL, new SchemaProcessor<Object>() {

        @Override
        public Object process(@NotNull String property, XmlElementDescriptor descriptor) {
            if (property.startsWith("project.")) {
                addVariant(result, property.substring("project.".length()), descriptor, prefix, MavenIcons.MavenLogo);
            }
            return null;
        }
    });
    processSchema(MavenSchemaProvider.MAVEN_SETTINGS_SCHEMA_URL, new SchemaProcessor<Object>() {

        @Override
        public Object process(@NotNull String property, XmlElementDescriptor descriptor) {
            result.add(createLookupElement(descriptor, property, MavenIcons.MavenLogo));
            return null;
        }
    });
    collectPropertiesVariants(result, variants);
    collectSystemEnvProperties(MavenPropertiesVirtualFileSystem.SYSTEM_PROPERTIES_FILE, null, result, variants);
    collectSystemEnvProperties(MavenPropertiesVirtualFileSystem.ENV_PROPERTIES_FILE, "env.", result, variants);
    MavenRunnerSettings runnerSettings = MavenRunner.getInstance(myProject).getSettings();
    for (String prop : runnerSettings.getMavenProperties().keySet()) {
        if (variants.add(prefix)) {
            result.add(LookupElementBuilder.create(prop).withIcon(PlatformIcons.PROPERTY_ICON));
        }
    }
    for (String prop : MavenUtil.getPropertiesFromMavenOpts().keySet()) {
        if (variants.add(prop)) {
            result.add(LookupElementBuilder.create(prop).withIcon(PlatformIcons.PROPERTY_ICON));
        }
    }
    for (Object key : myMavenProject.getProperties().keySet()) {
        if (key instanceof String) {
            String property = (String) key;
            if (variants.add(property)) {
                result.add(LookupElementBuilder.create(property).withIcon(PlatformIcons.PROPERTY_ICON));
            }
        }
    }
    MavenDomConfiguration pluginCfg = DomUtil.findDomElement(myElement, MavenDomConfiguration.class);
    if (pluginCfg != null) {
        MavenPluginDescriptor.processDescriptors(descriptor -> {
            if (descriptor.properties != null) {
                for (MavenPluginDescriptor.ModelProperty property : descriptor.properties) {
                    if (property.insideConfigurationOnly) {
                        result.add(LookupElementBuilder.create(property.name).withIcon(PlatformIcons.PROPERTY_ICON));
                    }
                }
            }
            return true;
        }, pluginCfg);
    }
}
Also used : MavenRunnerSettings(org.jetbrains.idea.maven.execution.MavenRunnerSettings) MavenId(org.jetbrains.idea.maven.model.MavenId) MavenDomConfiguration(org.jetbrains.idea.maven.dom.model.MavenDomConfiguration) MavenProject(org.jetbrains.idea.maven.project.MavenProject) MavenPluginDescriptor(org.jetbrains.idea.maven.plugins.api.MavenPluginDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor)

Example 2 with MavenRunnerSettings

use of org.jetbrains.idea.maven.execution.MavenRunnerSettings in project intellij-community by JetBrains.

the class MavenModuleBuilderHelper method generateFromArchetype.

private void generateFromArchetype(final Project project, final VirtualFile pom) {
    final File workingDir;
    try {
        workingDir = FileUtil.createTempDirectory("archetype", "tmp");
        workingDir.deleteOnExit();
    } catch (IOException e) {
        showError(project, e);
        return;
    }
    MavenRunnerParameters params = new MavenRunnerParameters(false, workingDir.getPath(), Collections.singletonList("org.apache.maven.plugins:maven-archetype-plugin:RELEASE:generate"), Collections.emptyList());
    MavenRunner runner = MavenRunner.getInstance(project);
    MavenRunnerSettings settings = runner.getState().clone();
    Map<String, String> props = settings.getMavenProperties();
    props.put("interactiveMode", "false");
    //props.put("archetypeGroupId", myArchetype.groupId);
    //props.put("archetypeArtifactId", myArchetype.artifactId);
    //props.put("archetypeVersion", myArchetype.version);
    //if (myArchetype.repository != null) props.put("archetypeRepository", myArchetype.repository);
    //props.put("groupId", myProjectId.getGroupId());
    //props.put("artifactId", myProjectId.getArtifactId());
    //props.put("version", myProjectId.getVersion());
    props.putAll(myPropertiesToCreateByArtifact);
    runner.run(params, settings, () -> copyGeneratedFiles(workingDir, pom, project));
}
Also used : MavenRunnerSettings(org.jetbrains.idea.maven.execution.MavenRunnerSettings) MavenRunner(org.jetbrains.idea.maven.execution.MavenRunner) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) MavenRunnerParameters(org.jetbrains.idea.maven.execution.MavenRunnerParameters)

Example 3 with MavenRunnerSettings

use of org.jetbrains.idea.maven.execution.MavenRunnerSettings in project intellij-community by JetBrains.

the class MavenPropertyPsiReference method doResolve.

// See org.apache.maven.project.interpolation.AbstractStringBasedModelInterpolator.createValueSources()
@Nullable
protected PsiElement doResolve() {
    boolean hasPrefix = false;
    String unprefixed = myText;
    if (myText.startsWith("pom.")) {
        unprefixed = myText.substring("pom.".length());
        hasPrefix = true;
    } else if (myText.startsWith("project.")) {
        unprefixed = myText.substring("project.".length());
        hasPrefix = true;
    }
    MavenProject mavenProject = myMavenProject;
    while (unprefixed.startsWith("parent.")) {
        if (unprefixed.equals("parent.groupId") || unprefixed.equals("parent.artifactId") || unprefixed.equals("parent.version") || unprefixed.equals("parent.relativePath")) {
            break;
        }
        MavenId parentId = mavenProject.getParentId();
        if (parentId == null)
            return null;
        mavenProject = myProjectsManager.findProject(parentId);
        if (mavenProject == null)
            return null;
        unprefixed = unprefixed.substring("parent.".length());
    }
    if (unprefixed.equals("basedir") || (hasPrefix && mavenProject == myMavenProject && unprefixed.equals("baseUri"))) {
        return getBaseDir(mavenProject);
    }
    if (myText.equals(TIMESTAMP_PROP)) {
        return myElement;
    }
    if (hasPrefix) {
        MavenDomProjectModel domProjectModel = MavenDomUtil.getMavenDomProjectModel(myProject, mavenProject.getFile());
        if (domProjectModel != null) {
            PsiElement res = resolveModelProperty(domProjectModel, unprefixed, new HashSet<>());
            if (res != null) {
                return res;
            }
        }
    }
    // todo resolve properties from config.
    MavenRunnerSettings runnerSettings = MavenRunner.getInstance(myProject).getSettings();
    if (runnerSettings.getMavenProperties().containsKey(myText) || runnerSettings.getVmOptions().contains("-D" + myText + '=')) {
        return myElement;
    }
    if (MavenUtil.getPropertiesFromMavenOpts().containsKey(myText)) {
        return myElement;
    }
    MavenDomProfile profile = DomUtil.findDomElement(myElement, MavenDomProfile.class);
    if (profile != null) {
        PsiElement result = MavenDomProjectProcessorUtils.findProperty(profile.getProperties(), myText);
        if (result != null)
            return result;
    }
    MavenDomConfiguration pluginCfg = DomUtil.findDomElement(myElement, MavenDomConfiguration.class);
    if (pluginCfg != null) {
        boolean notFound = MavenPluginDescriptor.processDescriptors(descriptor -> {
            if (descriptor.properties != null) {
                for (MavenPluginDescriptor.ModelProperty property : descriptor.properties) {
                    if (property.insideConfigurationOnly && property.name.equals(myText)) {
                        return false;
                    }
                }
            }
            return true;
        }, pluginCfg);
        if (!notFound) {
            return myElement;
        }
    }
    if (myProjectDom != null) {
        PsiElement result = MavenDomProjectProcessorUtils.searchProperty(myText, myProjectDom, myProject);
        if (result != null)
            return result;
    }
    if ("java.home".equals(myText)) {
        PsiElement element = resolveToCustomSystemProperty("java.home", MavenUtil.getModuleJreHome(myProjectsManager, mavenProject));
        if (element != null) {
            return element;
        }
    }
    if ("java.version".equals(myText)) {
        PsiElement element = resolveToCustomSystemProperty("java.version", MavenUtil.getModuleJavaVersion(myProjectsManager, mavenProject));
        if (element != null) {
            return element;
        }
    }
    MavenPropertiesVirtualFileSystem mavenPropertiesVirtualFileSystem = MavenPropertiesVirtualFileSystem.getInstance();
    IProperty property = mavenPropertiesVirtualFileSystem.findSystemProperty(myProject, myText);
    if (property != null)
        return property.getPsiElement();
    if (myText.startsWith("env.")) {
        property = mavenPropertiesVirtualFileSystem.findEnvProperty(myProject, myText.substring("env.".length()));
        if (property != null)
            return property.getPsiElement();
    }
    String textWithEnv = "env." + myText;
    property = mavenPropertiesVirtualFileSystem.findSystemProperty(myProject, textWithEnv);
    if (property != null)
        return property.getPsiElement();
    property = mavenPropertiesVirtualFileSystem.findEnvProperty(myProject, textWithEnv);
    if (property != null)
        return property.getPsiElement();
    if (!hasPrefix) {
        MavenDomProjectModel domProjectModel = MavenDomUtil.getMavenDomProjectModel(myProject, mavenProject.getFile());
        if (domProjectModel != null) {
            PsiElement res = resolveModelProperty(domProjectModel, unprefixed, new HashSet<>());
            if (res != null) {
                return res;
            }
        }
    }
    if (mavenProject.getProperties().containsKey(myText)) {
        return myElement;
    }
    if (myText.startsWith("settings.")) {
        return resolveSettingsModelProperty();
    }
    return null;
}
Also used : MavenRunnerSettings(org.jetbrains.idea.maven.execution.MavenRunnerSettings) MavenPropertiesVirtualFileSystem(org.jetbrains.idea.maven.vfs.MavenPropertiesVirtualFileSystem) MavenDomProfile(org.jetbrains.idea.maven.dom.model.MavenDomProfile) MavenId(org.jetbrains.idea.maven.model.MavenId) MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) MavenDomConfiguration(org.jetbrains.idea.maven.dom.model.MavenDomConfiguration) MavenProject(org.jetbrains.idea.maven.project.MavenProject) IProperty(com.intellij.lang.properties.IProperty) MavenPluginDescriptor(org.jetbrains.idea.maven.plugins.api.MavenPluginDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with MavenRunnerSettings

use of org.jetbrains.idea.maven.execution.MavenRunnerSettings in project intellij-community by JetBrains.

the class ArchetypesTest method testGenerating.

public void testGenerating() throws Exception {
    if (!hasMavenInstallation())
        return;
    File dir = new File(myDir.getPath(), "generated");
    dir.mkdirs();
    MavenRunnerParameters params = new MavenRunnerParameters(false, dir.getPath(), Arrays.asList("org.apache.maven.plugins:maven-archetype-plugin:RELEASE:generate"), Collections.<String>emptyList());
    MavenRunnerSettings settings = new MavenRunnerSettings();
    Map<String, String> props = new THashMap<>();
    props.put("archetypeGroupId", "org.apache.maven.archetypes");
    props.put("archetypeArtifactId", "maven-archetype-quickstart");
    props.put("archetypeVersion", "1.0");
    props.put("interactiveMode", "false");
    props.put("groupId", "foo");
    props.put("groupId", "foo");
    props.put("artifactId", "bar");
    settings.setMavenProperties(props);
    MavenExecutor exec;
    settings.setJreName(MavenRunnerSettings.USE_INTERNAL_JAVA);
    exec = new MavenExternalExecutor(myProject, params, getMavenGeneralSettings(), settings, NULL_MAVEN_CONSOLE);
    exec.execute(new EmptyProgressIndicator());
    assertTrue(new File(dir, "bar/pom.xml").exists());
}
Also used : MavenRunnerSettings(org.jetbrains.idea.maven.execution.MavenRunnerSettings) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) THashMap(gnu.trove.THashMap) MavenExecutor(org.jetbrains.idea.maven.execution.MavenExecutor) File(java.io.File) MavenExternalExecutor(org.jetbrains.idea.maven.execution.MavenExternalExecutor) MavenRunnerParameters(org.jetbrains.idea.maven.execution.MavenRunnerParameters)

Aggregations

MavenRunnerSettings (org.jetbrains.idea.maven.execution.MavenRunnerSettings)4 File (java.io.File)2 MavenDomConfiguration (org.jetbrains.idea.maven.dom.model.MavenDomConfiguration)2 MavenRunnerParameters (org.jetbrains.idea.maven.execution.MavenRunnerParameters)2 MavenId (org.jetbrains.idea.maven.model.MavenId)2 MavenPluginDescriptor (org.jetbrains.idea.maven.plugins.api.MavenPluginDescriptor)2 MavenProject (org.jetbrains.idea.maven.project.MavenProject)2 IProperty (com.intellij.lang.properties.IProperty)1 EmptyProgressIndicator (com.intellij.openapi.progress.EmptyProgressIndicator)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)1 THashMap (gnu.trove.THashMap)1 IOException (java.io.IOException)1 Nullable (org.jetbrains.annotations.Nullable)1 MavenDomProfile (org.jetbrains.idea.maven.dom.model.MavenDomProfile)1 MavenDomProjectModel (org.jetbrains.idea.maven.dom.model.MavenDomProjectModel)1 MavenExecutor (org.jetbrains.idea.maven.execution.MavenExecutor)1 MavenExternalExecutor (org.jetbrains.idea.maven.execution.MavenExternalExecutor)1 MavenRunner (org.jetbrains.idea.maven.execution.MavenRunner)1