use of org.jetbrains.idea.maven.project.MavenProject in project intellij-community by JetBrains.
the class MavenProjectModelModifier method addDependency.
private Promise<Void> addDependency(@NotNull Collection<Module> fromModules, @NotNull final MavenId mavenId, @Nullable String minVersion, @Nullable String maxVersion, @NotNull final DependencyScope scope) {
final List<Trinity<MavenDomProjectModel, MavenId, String>> models = new ArrayList<>(fromModules.size());
List<XmlFile> files = new ArrayList<>(fromModules.size());
List<MavenProject> projectToUpdate = new ArrayList<>(fromModules.size());
final String mavenScope = getMavenScope(scope);
for (Module from : fromModules) {
if (!myProjectsManager.isMavenizedModule(from))
return null;
MavenProject fromProject = myProjectsManager.findProject(from);
if (fromProject == null)
return null;
final MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(myProject, fromProject.getFile());
if (model == null)
return null;
String scopeToSet = null;
String version = null;
if (mavenId.getGroupId() != null && mavenId.getArtifactId() != null) {
MavenDomDependency managedDependency = MavenDependencyCompletionUtil.findManagedDependency(model, myProject, mavenId.getGroupId(), mavenId.getArtifactId());
if (managedDependency != null) {
String managedScope = StringUtil.nullize(managedDependency.getScope().getStringValue(), true);
scopeToSet = (managedScope == null && MavenConstants.SCOPE_COMPILE.equals(mavenScope)) || StringUtil.equals(managedScope, mavenScope) ? null : mavenScope;
}
if (managedDependency == null || StringUtil.isEmpty(managedDependency.getVersion().getStringValue())) {
version = selectVersion(mavenId, minVersion, maxVersion);
}
}
models.add(Trinity.create(model, new MavenId(mavenId.getGroupId(), mavenId.getArtifactId(), version), scopeToSet));
files.add(DomUtil.getFile(model));
projectToUpdate.add(fromProject);
}
new WriteCommandAction(myProject, "Add Maven Dependency", PsiUtilCore.toPsiFileArray(files)) {
@Override
protected void run(@NotNull Result result) throws Throwable {
for (Trinity<MavenDomProjectModel, MavenId, String> trinity : models) {
final MavenDomProjectModel model = trinity.first;
MavenDomDependency dependency = MavenDomUtil.createDomDependency(model, null, trinity.second);
String mavenScope = trinity.third;
if (mavenScope != null) {
dependency.getScope().setStringValue(mavenScope);
}
Document document = PsiDocumentManager.getInstance(myProject).getDocument(DomUtil.getFile(model));
if (document != null) {
FileDocumentManager.getInstance().saveDocument(document);
}
}
}
}.execute();
return myProjectsManager.forceUpdateProjects(projectToUpdate);
}
use of org.jetbrains.idea.maven.project.MavenProject in project intellij-community by JetBrains.
the class MavenProjectModelModifier method changeLanguageLevel.
@Override
public Promise<Void> changeLanguageLevel(@NotNull Module module, @NotNull final LanguageLevel level) {
if (!myProjectsManager.isMavenizedModule(module))
return null;
MavenProject mavenProject = myProjectsManager.findProject(module);
if (mavenProject == null)
return null;
final MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(myProject, mavenProject.getFile());
if (model == null)
return null;
new WriteCommandAction(myProject, "Add Maven Dependency", DomUtil.getFile(model)) {
@Override
protected void run(@NotNull Result result) throws Throwable {
MavenDomConfiguration configuration = getCompilerPlugin(model).getConfiguration();
XmlTag tag = configuration.ensureTagExists();
setChildTagValue(tag, "source", level.getCompilerComplianceDefaultOption());
setChildTagValue(tag, "target", level.getCompilerComplianceDefaultOption());
Document document = PsiDocumentManager.getInstance(myProject).getDocument(DomUtil.getFile(model));
if (document != null) {
FileDocumentManager.getInstance().saveDocument(document);
}
}
}.execute();
return myProjectsManager.forceUpdateProjects(Collections.singleton(mavenProject));
}
use of org.jetbrains.idea.maven.project.MavenProject in project intellij-community by JetBrains.
the class MavenModuleNameMapper method resolveModuleNames.
private static void resolveModuleNames(Collection<MavenProject> projects, Map<MavenProject, Module> mavenProjectToModule, Map<MavenProject, String> mavenProjectToModuleName) {
NameItem[] names = new NameItem[projects.size()];
int i = 0;
for (MavenProject each : projects) {
names[i++] = new NameItem(each, mavenProjectToModule.get(each));
}
Arrays.sort(names);
Map<String, Integer> nameCounters = new HashMap<>();
for (i = 0; i < names.length; i++) {
if (names[i].hasDuplicatedGroup)
continue;
for (int k = i + 1; k < names.length; k++) {
if (names[i].originalName.equals(names[k].originalName)) {
nameCounters.put(names[i].originalName, 0);
if (names[i].groupId.equals(names[k].groupId)) {
names[i].hasDuplicatedGroup = true;
names[k].hasDuplicatedGroup = true;
}
}
}
}
Set<String> existingNames = new HashSet<>();
for (NameItem name : names) {
if (name.module != null) {
boolean wasAdded = existingNames.add(name.getResultName());
//assert wasAdded : name.getResultName();
}
}
for (NameItem nameItem : names) {
if (nameItem.module == null) {
Integer c = nameCounters.get(nameItem.originalName);
if (c != null) {
nameItem.number = c;
nameCounters.put(nameItem.originalName, c + 1);
}
do {
String name = nameItem.getResultName();
if (existingNames.add(name))
break;
nameItem.number++;
nameCounters.put(nameItem.originalName, nameItem.number + 1);
} while (true);
}
}
for (NameItem each : names) {
mavenProjectToModuleName.put(each.project, each.getResultName());
}
//assert new HashSet<String>(mavenProjectToModuleName.values()).size() == mavenProjectToModuleName.size() : new HashMap<MavenProject, String>(mavenProjectToModuleName);
}
use of org.jetbrains.idea.maven.project.MavenProject in project intellij-community by JetBrains.
the class MavenModuleNameMapper method resolveModulePaths.
private static void resolveModulePaths(Collection<MavenProject> projects, Map<MavenProject, Module> mavenProjectToModule, Map<MavenProject, String> mavenProjectToModuleName, Map<MavenProject, String> mavenProjectToModulePath, String dedicatedModuleDir) {
for (MavenProject each : projects) {
Module module = mavenProjectToModule.get(each);
String path = module != null ? module.getModuleFilePath() : generateModulePath(each, mavenProjectToModuleName, dedicatedModuleDir);
mavenProjectToModulePath.put(each, path);
}
}
use of org.jetbrains.idea.maven.project.MavenProject in project intellij-community by JetBrains.
the class ToggleBeforeRunTaskAction method doIsSelected.
@Override
protected boolean doIsSelected(AnActionEvent e) {
final DataContext context = e.getDataContext();
final Pair<MavenProject, String> desc = getTaskDesc(context);
if (desc != null) {
final RunManagerEx runManager = getRunManager(context);
if (runManager == null)
return false;
for (MavenBeforeRunTask each : runManager.getBeforeRunTasks(MavenBeforeRunTasksProvider.ID)) {
if (each.isFor(desc.first, desc.second))
return true;
}
}
return false;
}
Aggregations