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);
}
}
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);
}
}
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);
}
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;
}
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);
}
}
}
}
Aggregations