Search in sources :

Example 21 with MavenId

use of org.jetbrains.idea.maven.model.MavenId in project android by JetBrains.

the class AndroidFacetImporterBase method importSdkAndDependenciesForApklibArtifact.

private static void importSdkAndDependenciesForApklibArtifact(Project project, ModifiableRootModel apklibModuleModel, IdeModifiableModelsProvider modelsProvider, MavenProjectsTree mavenTree, MavenId artifactMavenId, Map<MavenProject, String> mavenProject2ModuleName) {
    final String apklibModuleName = apklibModuleModel.getModule().getName();
    final AndroidExternalApklibDependenciesManager adm = AndroidExternalApklibDependenciesManager.getInstance(project);
    final MavenArtifactResolvedInfo resolvedInfo = adm.getResolvedInfoForArtifact(artifactMavenId);
    for (OrderEntry entry : apklibModuleModel.getOrderEntries()) {
        if (entry instanceof ModuleOrderEntry || entry instanceof LibraryOrderEntry) {
            apklibModuleModel.removeOrderEntry(entry);
        }
    }
    if (resolvedInfo != null) {
        final String apiLevel = resolvedInfo.getApiLevel();
        final Sdk sdk = findOrCreateAndroidPlatform(apiLevel, null);
        if (sdk != null) {
            apklibModuleModel.setSdk(sdk);
        } else {
            reportCannotFindAndroidPlatformError(apklibModuleName, apiLevel, project);
        }
        for (AndroidExternalApklibDependenciesManager.MavenDependencyInfo depArtifactInfo : resolvedInfo.getDependencies()) {
            final MavenId depMavenId = new MavenId(depArtifactInfo.getGroupId(), depArtifactInfo.getArtifactId(), depArtifactInfo.getVersion());
            final String type = depArtifactInfo.getType();
            final String scope = depArtifactInfo.getScope();
            final String path = depArtifactInfo.getPath();
            final String libName = depArtifactInfo.getLibName();
            if (AndroidMavenUtil.APKLIB_DEPENDENCY_AND_PACKAGING_TYPE.equals(type) && MavenConstants.SCOPE_COMPILE.equals(scope)) {
                final MavenProject depProject = mavenTree.findProject(depMavenId);
                if (depProject != null) {
                    final String depModuleName = mavenProject2ModuleName.get(depProject);
                    if (depModuleName != null) {
                        addModuleDependency(modelsProvider, apklibModuleModel, depModuleName, DependencyScope.COMPILE);
                    }
                } else {
                    final String depApklibGenModuleName = AndroidMavenUtil.getModuleNameForExtApklibArtifact(depMavenId);
                    addModuleDependency(modelsProvider, apklibModuleModel, depApklibGenModuleName, DependencyScope.COMPILE);
                }
            } else {
                final DependencyScope depScope = MavenModuleImporter.selectScope(scope);
                if (scope != null) {
                    addLibraryDependency(libName, depScope, modelsProvider, apklibModuleModel, path);
                } else {
                    LOG.info("Unknown Maven scope " + depScope);
                }
            }
        }
    } else {
        AndroidUtils.reportImportErrorToEventLog("Cannot find sdk info for artifact " + artifactMavenId.getKey(), apklibModuleName, project);
    }
}
Also used : MavenId(org.jetbrains.idea.maven.model.MavenId) Sdk(com.intellij.openapi.projectRoots.Sdk)

Example 22 with MavenId

use of org.jetbrains.idea.maven.model.MavenId in project android by JetBrains.

the class AndroidFacetImporterBase method importExternalAarDependency.

private static void importExternalAarDependency(@NotNull MavenArtifact artifact, @NotNull MavenProject mavenProject, @NotNull MavenProjectsTree mavenTree, @NotNull MavenRootModelAdapter rootModelAdapter, @NotNull IdeModifiableModelsProvider modelsProvider, @NotNull Project project, @NotNull List<MavenProjectsProcessorTask> postTasks) {
    final Library aarLibrary = rootModelAdapter.findLibrary(artifact);
    if (aarLibrary == null) {
        return;
    }
    final MavenId mavenId = artifact.getMavenId();
    Map<MavenId, String> importedAarArtifacts = project.getUserData(IMPORTED_AAR_ARTIFACTS);
    if (importedAarArtifacts == null) {
        importedAarArtifacts = new HashMap<MavenId, String>();
        project.putUserData(IMPORTED_AAR_ARTIFACTS, importedAarArtifacts);
        postTasks.add(new MavenProjectsProcessorTask() {

            @Override
            public void perform(Project project, MavenEmbeddersManager embeddersManager, MavenConsole console, MavenProgressIndicator indicator) throws MavenProcessCanceledException {
                project.putUserData(IMPORTED_AAR_ARTIFACTS, null);
            }
        });
    }
    final List<MavenProject> allProjects = mavenTree.getProjects();
    String aarDirPath = importedAarArtifacts.get(mavenId);
    if (aarDirPath == null) {
        final String aarDirName = AndroidMavenUtil.getMavenIdStringForFileName(mavenId);
        aarDirPath = findExtractedAarDirectory(allProjects, aarDirName);
        if (aarDirPath == null) {
            final String genDirPath = AndroidMavenUtil.computePathForGenExternalApklibsDir(mavenId, mavenProject, allProjects);
            if (genDirPath == null) {
                return;
            }
            aarDirPath = genDirPath + "/" + aarDirName;
        }
        importedAarArtifacts.put(mavenId, aarDirPath);
        extractArtifact(artifact.getPath(), aarDirPath, project, mavenProject.getName());
    }
    final Library.ModifiableModel aarLibModel = modelsProvider.getModifiableLibraryModel(aarLibrary);
    final String classesJarPath = aarDirPath + "/" + SdkConstants.FN_CLASSES_JAR;
    final String classesJarUrl = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, classesJarPath) + JarFileSystem.JAR_SEPARATOR;
    final String resDirUrl = VfsUtilCore.pathToUrl(aarDirPath + "/" + SdkConstants.FD_RES);
    final Set<String> urlsToAdd = new HashSet<String>(Arrays.asList(classesJarUrl, resDirUrl));
    collectJarsInAarLibsFolder(aarDirPath, urlsToAdd);
    for (String url : aarLibModel.getUrls(OrderRootType.CLASSES)) {
        if (!urlsToAdd.remove(url)) {
            aarLibModel.removeRoot(url, OrderRootType.CLASSES);
        }
    }
    for (String url : urlsToAdd) {
        aarLibModel.addRoot(url, OrderRootType.CLASSES);
    }
}
Also used : MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) Project(com.intellij.openapi.project.Project) MavenId(org.jetbrains.idea.maven.model.MavenId) MavenProgressIndicator(org.jetbrains.idea.maven.utils.MavenProgressIndicator) Library(com.intellij.openapi.roots.libraries.Library) HashSet(com.intellij.util.containers.HashSet)

Example 23 with MavenId

use of org.jetbrains.idea.maven.model.MavenId in project android by JetBrains.

the class AndroidFacetImporterBase method doResolveApklibArtifact.

private void doResolveApklibArtifact(Project project, MavenArtifact artifact, MavenEmbedderWrapper embedder, MavenProjectsManager mavenProjectsManager, String moduleName, AndroidExternalApklibDependenciesManager adm, ResolveContext context) throws MavenProcessCanceledException {
    final File depArtifacetFile = new File(FileUtil.getNameWithoutExtension(artifact.getPath()) + ".pom");
    if (!depArtifacetFile.exists()) {
        AndroidUtils.reportImportErrorToEventLog("Cannot find file " + depArtifacetFile.getPath(), moduleName, project);
        return;
    }
    final VirtualFile vDepArtifactFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(depArtifacetFile);
    if (vDepArtifactFile == null) {
        AndroidUtils.reportImportErrorToEventLog("Cannot find file " + depArtifacetFile.getPath() + " in VFS", moduleName, project);
        return;
    }
    final MavenProject projectForExternalApklib = new MavenProject(vDepArtifactFile);
    final MavenGeneralSettings generalSettings = mavenProjectsManager.getGeneralSettings();
    final MavenProjectReader mavenProjectReader = new MavenProjectReader(project);
    final MavenProjectReaderProjectLocator locator = new MavenProjectReaderProjectLocator() {

        @Nullable
        @Override
        public VirtualFile findProjectFile(MavenId coordinates) {
            return null;
        }
    };
    final MavenArtifactResolvedInfo info = new MavenArtifactResolvedInfo();
    final MavenId mavenId = artifact.getMavenId();
    adm.setResolvedInfoForArtifact(mavenId, info);
    projectForExternalApklib.read(generalSettings, mavenProjectsManager.getExplicitProfiles(), mavenProjectReader, locator);
    projectForExternalApklib.resolve(project, generalSettings, embedder, mavenProjectReader, locator, context);
    final String apiLevel = getPlatformFromConfig(projectForExternalApklib);
    final List<AndroidExternalApklibDependenciesManager.MavenDependencyInfo> dependencies = new ArrayList<AndroidExternalApklibDependenciesManager.MavenDependencyInfo>();
    List<MavenArtifact> deps = projectForExternalApklib.getDependencies();
    if (deps.isEmpty()) {
        // Hack for solving IDEA-119450. Maven reports "unknown packaging 'apklib'" when resolving if android plugin is not specified
        // in the "build" section of the pom, so we create fake jar artifact dependent on the apklib artifact and resolve it
        final File fakePomFile = buildFakeArtifactPomFile(artifact, moduleName, project);
        if (fakePomFile != null) {
            try {
                final VirtualFile vFakePomFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(fakePomFile);
                if (vFakePomFile != null) {
                    final MavenProject fakeProject = new MavenProject(vFakePomFile);
                    fakeProject.read(generalSettings, mavenProjectsManager.getExplicitProfiles(), mavenProjectReader, locator);
                    fakeProject.resolve(project, generalSettings, embedder, mavenProjectReader, locator, context);
                    deps = fakeProject.getDependencies();
                    for (Iterator<MavenArtifact> it = deps.iterator(); it.hasNext(); ) {
                        final MavenArtifact dep = it.next();
                        if (dep.getMavenId().equals(mavenId)) {
                            it.remove();
                        }
                    }
                } else {
                    LOG.error("Cannot find file " + fakePomFile.getPath() + " in the VFS");
                }
            } finally {
                FileUtil.delete(fakePomFile);
            }
        }
    }
    for (MavenArtifact depArtifact : deps) {
        dependencies.add(AndroidExternalApklibDependenciesManager.MavenDependencyInfo.create(depArtifact));
    }
    info.setApiLevel(apiLevel != null ? apiLevel : "");
    info.setDependencies(dependencies);
}
Also used : MavenId(org.jetbrains.idea.maven.model.MavenId) File(java.io.File) MavenArtifact(org.jetbrains.idea.maven.model.MavenArtifact)

Example 24 with MavenId

use of org.jetbrains.idea.maven.model.MavenId in project android by JetBrains.

the class AndroidFacetImporterBase method doImportExternalApklibDependency.

private static String doImportExternalApklibDependency(Project project, IdeModifiableModelsProvider modelsProvider, MavenProjectsTree mavenTree, MavenProject mavenProject, Map<MavenProject, String> mavenProject2ModuleName, List<MavenProjectsProcessorTask> tasks, AndroidExternalApklibDependenciesManager.MavenDependencyInfo depInfo) {
    final MavenId depArtifactMavenId = new MavenId(depInfo.getGroupId(), depInfo.getArtifactId(), depInfo.getVersion());
    final ModifiableModuleModel moduleModel = modelsProvider.getModifiableModuleModel();
    final String apklibModuleName = AndroidMavenUtil.getModuleNameForExtApklibArtifact(depArtifactMavenId);
    Module apklibModule = moduleModel.findModuleByName(apklibModuleName);
    if ((apklibModule == null || apklibModule.getUserData(MODULE_IMPORTED) == null) && MavenConstants.SCOPE_COMPILE.equals(depInfo.getScope())) {
        apklibModule = importExternalApklibArtifact(project, apklibModule, modelsProvider, mavenProject, mavenTree, depArtifactMavenId, depInfo.getPath(), moduleModel, mavenProject2ModuleName);
        if (apklibModule != null) {
            apklibModule.putUserData(MODULE_IMPORTED, Boolean.TRUE);
            final Module finalGenModule = apklibModule;
            tasks.add(new MavenProjectsProcessorTask() {

                @Override
                public void perform(Project project, MavenEmbeddersManager embeddersManager, MavenConsole console, MavenProgressIndicator indicator) throws MavenProcessCanceledException {
                    finalGenModule.putUserData(MODULE_IMPORTED, null);
                }
            });
            final MavenArtifactResolvedInfo resolvedDepArtifact = AndroidExternalApklibDependenciesManager.getInstance(project).getResolvedInfoForArtifact(depArtifactMavenId);
            if (resolvedDepArtifact != null) {
                for (AndroidExternalApklibDependenciesManager.MavenDependencyInfo depDepInfo : resolvedDepArtifact.getDependencies()) {
                    final MavenId depDepMavenId = new MavenId(depDepInfo.getGroupId(), depDepInfo.getArtifactId(), depDepInfo.getVersion());
                    if (AndroidMavenUtil.APKLIB_DEPENDENCY_AND_PACKAGING_TYPE.equals(depDepInfo.getType()) && mavenTree.findProject(depDepMavenId) == null) {
                        doImportExternalApklibDependency(project, modelsProvider, mavenTree, mavenProject, mavenProject2ModuleName, tasks, depDepInfo);
                    }
                }
            } else {
                AndroidUtils.reportImportErrorToEventLog("Cannot find resolved info for artifact " + depArtifactMavenId.getKey(), apklibModuleName, project);
            }
        }
    }
    return apklibModuleName;
}
Also used : MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) Project(com.intellij.openapi.project.Project) MavenId(org.jetbrains.idea.maven.model.MavenId) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) MavenProgressIndicator(org.jetbrains.idea.maven.utils.MavenProgressIndicator) Module(com.intellij.openapi.module.Module)

Example 25 with MavenId

use of org.jetbrains.idea.maven.model.MavenId in project android by JetBrains.

the class AndroidFacetImporterBase method resolve.

@Override
public void resolve(final Project project, MavenProject mavenProject, NativeMavenProjectHolder nativeMavenProject, MavenEmbedderWrapper embedder, ResolveContext context) throws MavenProcessCanceledException {
    final AndroidExternalApklibDependenciesManager adm = AndroidExternalApklibDependenciesManager.getInstance(project);
    for (MavenArtifact depArtifact : mavenProject.getDependencies()) {
        final MavenProjectsManager mavenProjectsManager = MavenProjectsManager.getInstance(project);
        if (AndroidMavenUtil.APKLIB_DEPENDENCY_AND_PACKAGING_TYPE.equals(depArtifact.getType()) && mavenProjectsManager.findProject(depArtifact) == null && MavenConstants.SCOPE_COMPILE.equals(depArtifact.getScope())) {
            Set<MavenId> resolvedArtifacts = context.getUserData(RESOLVED_APKLIB_ARTIFACTS_KEY);
            if (resolvedArtifacts == null) {
                resolvedArtifacts = new HashSet<MavenId>();
                context.putUserData(RESOLVED_APKLIB_ARTIFACTS_KEY, resolvedArtifacts);
            }
            if (resolvedArtifacts.add(depArtifact.getMavenId())) {
                doResolveApklibArtifact(project, depArtifact, embedder, mavenProjectsManager, mavenProject.getName(), adm, context);
            }
        }
    }
}
Also used : MavenId(org.jetbrains.idea.maven.model.MavenId) MavenArtifact(org.jetbrains.idea.maven.model.MavenArtifact)

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