Search in sources :

Example 26 with MavenId

use of org.jetbrains.idea.maven.model.MavenId 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 27 with MavenId

use of org.jetbrains.idea.maven.model.MavenId in project intellij-community by JetBrains.

the class AddMavenDependencyQuickFix method invoke.

public void invoke(@NotNull final Project project, Editor editor, final PsiFile file) throws IncorrectOperationException {
    if (!myRef.isValid())
        return;
    MavenProject mavenProject = MavenDomUtil.findContainingProject(file);
    if (mavenProject == null)
        return;
    final List<MavenId> ids = MavenArtifactSearchDialog.searchForClass(project, getReferenceText());
    if (ids.isEmpty())
        return;
    final MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(project, mavenProject.getFile());
    if (model == null)
        return;
    new WriteCommandAction(project, "Add Maven Dependency", DomUtil.getFile(model)) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            boolean isTestSource = false;
            VirtualFile virtualFile = file.getOriginalFile().getVirtualFile();
            if (virtualFile != null) {
                isTestSource = ProjectRootManager.getInstance(project).getFileIndex().isInTestSourceContent(virtualFile);
            }
            for (MavenId each : ids) {
                MavenDomDependency dependency = MavenDomUtil.createDomDependency(model, null, each);
                if (isTestSource) {
                    dependency.getScope().setStringValue("test");
                }
            }
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) VirtualFile(com.intellij.openapi.vfs.VirtualFile) MavenId(org.jetbrains.idea.maven.model.MavenId) MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) MavenProject(org.jetbrains.idea.maven.project.MavenProject) Result(com.intellij.openapi.application.Result) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency)

Example 28 with MavenId

use of org.jetbrains.idea.maven.model.MavenId in project intellij-community by JetBrains.

the class JUnit4IntegrationTest method before.

@Before
public void before() throws Throwable {
    EdtTestUtil.runInEdtAndWait(() -> {
        setUp();
        Module module = createEmptyModule();
        String communityPath = PlatformTestUtil.getCommunityPath().replace(File.separatorChar, '/');
        String methodName = myNameRule.getMethodName();
        methodName = methodName.substring(0, methodName.indexOf("["));
        String testDataPath = communityPath + File.separator + "plugins" + File.separator + "junit5_rt_tests" + File.separator + "testData" + File.separator + "integration" + File.separator + methodName;
        MavenId mavenId = new MavenId("junit", "junit", myJUnitVersion);
        MavenRepositoryInfo repositoryInfo = new MavenRepositoryInfo("maven", "maven", "http://maven.labs.intellij.net/repo1");
        RepositoryAttachHandler.doResolveInner(getProject(), mavenId, Collections.emptyList(), Collections.singleton(repositoryInfo), artifacts -> {
            for (MavenArtifact artifact : artifacts) {
                ModuleRootModificationUtil.addModuleLibrary(module, VfsUtilCore.pathToUrl(artifact.getPath()));
            }
            return true;
        }, new DumbProgressIndicator());
        ModuleRootModificationUtil.setModuleSdk(module, JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk());
        ModuleRootModificationUtil.updateModel(module, model -> {
            ContentEntry contentEntry = model.addContentEntry(VfsUtilCore.pathToUrl(testDataPath));
            contentEntry.addSourceFolder(VfsUtilCore.pathToUrl(testDataPath + File.separator + "test"), true);
            CompilerModuleExtension moduleExtension = model.getModuleExtension(CompilerModuleExtension.class);
            moduleExtension.inheritCompilerOutputPath(false);
            moduleExtension.setCompilerOutputPathForTests(VfsUtilCore.pathToUrl(testDataPath + File.separator + "out"));
        });
    });
}
Also used : MavenId(org.jetbrains.idea.maven.model.MavenId) ContentEntry(com.intellij.openapi.roots.ContentEntry) MavenRepositoryInfo(org.jetbrains.idea.maven.model.MavenRepositoryInfo) Module(com.intellij.openapi.module.Module) DumbProgressIndicator(com.intellij.openapi.progress.DumbProgressIndicator) MavenArtifact(org.jetbrains.idea.maven.model.MavenArtifact) CompilerModuleExtension(com.intellij.openapi.roots.CompilerModuleExtension) Before(org.junit.Before)

Example 29 with MavenId

use of org.jetbrains.idea.maven.model.MavenId in project intellij-community by JetBrains.

the class MavenFrameworkSupportProvider method createConfigurable.

@NotNull
@Override
public FrameworkSupportConfigurable createConfigurable(@NotNull FrameworkSupportModel model) {
    return new FrameworkSupportConfigurable() {

        @Override
        public JComponent getComponent() {
            return new JPanel();
        }

        @Override
        public void addSupport(@NotNull Module module, @NotNull ModifiableRootModel model, @Nullable Library library) {
            VirtualFile[] roots = model.getContentRoots();
            VirtualFile root;
            if (roots.length == 0) {
                VirtualFile moduleFile = module.getModuleFile();
                if (moduleFile != null) {
                    root = moduleFile.getParent();
                    model.addContentEntry(root);
                } else {
                    return;
                }
            } else {
                root = roots[0];
            }
            VirtualFile existingPom = null;
            for (VirtualFile child : root.getChildren()) {
                if (child.getName().startsWith("pom.")) {
                    existingPom = child;
                }
            }
            if (existingPom != null) {
                MavenProjectsManager.getInstance(module.getProject()).addManagedFilesOrUnignore(Collections.singletonList(existingPom));
            } else {
                prepareProjectStructure(model, root);
                new MavenModuleBuilderHelper(new MavenId("groupId", module.getName(), "1.0-SNAPSHOT"), null, null, false, false, null, null, "Add Maven Support").configure(model.getProject(), root, true);
            }
        }
    };
}
Also used : ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) MavenId(org.jetbrains.idea.maven.model.MavenId) FrameworkSupportConfigurable(com.intellij.ide.util.frameworkSupport.FrameworkSupportConfigurable) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with MavenId

use of org.jetbrains.idea.maven.model.MavenId in project intellij-community by JetBrains.

the class MavenModuleWizardStep method updateDataModel.

@Override
public void updateDataModel() {
    myContext.setProjectBuilder(myBuilder);
    myBuilder.setAggregatorProject(myAggregator);
    myBuilder.setParentProject(myParent);
    myBuilder.setProjectId(new MavenId(myGroupIdField.getText(), myArtifactIdField.getText(), myVersionField.getText()));
    myBuilder.setInheritedOptions(myInheritGroupIdCheckBox.isSelected(), myInheritVersionCheckBox.isSelected());
    if (myContext.getProjectName() == null) {
        myContext.setProjectName(myBuilder.getProjectId().getArtifactId());
    }
    if (myArchetypes != null) {
        myBuilder.setArchetype(myArchetypes.getSelectedArchetype());
    }
}
Also used : MavenId(org.jetbrains.idea.maven.model.MavenId)

Aggregations

MavenId (org.jetbrains.idea.maven.model.MavenId)59 MavenProject (org.jetbrains.idea.maven.project.MavenProject)16 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 Module (com.intellij.openapi.module.Module)5 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)5 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)4 IOException (java.io.IOException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 RemoteException (java.rmi.RemoteException)4 ArchetypeDataSourceException (org.apache.maven.archetype.source.ArchetypeDataSourceException)4 SoutMavenConsole (org.jetbrains.idea.maven.execution.SoutMavenConsole)4 MavenArtifact (org.jetbrains.idea.maven.model.MavenArtifact)4 MavenServerExecutionResult (org.jetbrains.idea.maven.server.MavenServerExecutionResult)4 Project (com.intellij.openapi.project.Project)3 File (java.io.File)3 MavenArchetype (org.jetbrains.idea.maven.model.MavenArchetype)3 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)2 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2