IDEA-90325 get version of apklib's dependency from any dependent android project

This commit is contained in:
Eugene Kudelevsky
2012-08-27 21:40:19 +04:00
parent a3b52b6e93
commit 8c53d8790e
2 changed files with 63 additions and 36 deletions
@@ -151,18 +151,16 @@ public class AndroidExternalApklibDependenciesManager implements PersistentState
private String myArtifactId;
private String myVersion;
private String myType;
private String myLibraryName;
private String myScope;
public MavenDependencyInfo() {
}
public MavenDependencyInfo(@NotNull MavenId mavenId, @NotNull String type, @NotNull String scope, @NotNull String libraryName) {
public MavenDependencyInfo(@NotNull MavenId mavenId, @NotNull String type, @NotNull String scope) {
myGroupId = mavenId.getGroupId();
myArtifactId = mavenId.getArtifactId();
myVersion = mavenId.getVersion();
myType = type;
myLibraryName = libraryName;
myScope = scope;
}
@@ -182,10 +180,6 @@ public class AndroidExternalApklibDependenciesManager implements PersistentState
return myType;
}
public String getLibraryName() {
return myLibraryName;
}
public String getScope() {
return myScope;
}
@@ -206,10 +200,6 @@ public class AndroidExternalApklibDependenciesManager implements PersistentState
myType = type;
}
public void setLibraryName(String libraryName) {
myLibraryName = libraryName;
}
public void setScope(String scope) {
myScope = scope;
}
@@ -31,6 +31,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.*;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.io.FileUtil;
@@ -409,13 +410,13 @@ public abstract class AndroidFacetImporterBase extends FacetImporter<AndroidFace
configuration.MANIFEST_FILE_RELATIVE_PATH = s + AndroidMavenUtil.APK_LIB_ARTIFACT_MANIFEST_FILE;
}
importSdkAndDependenciesForApklibArtifact(project, rootModelAdapter, apklibModuleModel, modelsProvider, mavenTree, artifact,
mavenProject2ModuleName);
importSdkAndDependenciesForApklibArtifact(project, mavenProject, rootModelAdapter, apklibModuleModel, modelsProvider,
mavenTree, artifact, mavenProject2ModuleName);
return apklibModule;
}
private static void importSdkAndDependenciesForApklibArtifact(Project project,
MavenProject mavenProject,
MavenRootModelAdapter rootModelAdapter,
ModifiableRootModel apklibModuleModel,
MavenModifiableModelsProvider modelsProvider,
@@ -467,30 +468,43 @@ public abstract class AndroidFacetImporterBase extends FacetImporter<AndroidFace
}
}
else {
final String libraryName = depArtifactInfo.getLibraryName();
final ModifiableRootModel rootModel = rootModelAdapter.getRootModel();
final LibraryOrderEntry libEntry = findLibraryByName(rootModel, libraryName);
if (libEntry != null) {
final Library library = libEntry.getLibrary();
if (library != null && apklibModuleModel.findLibraryOrderEntry(library) == null) {
final LibraryOrderEntry newLibEntry = apklibModuleModel.addLibraryEntry(library);
newLibEntry.setExported(libEntry.isExported());
newLibEntry.setScope(libEntry.getScope());
}
}
else {
final String message = "Cannot find library " + libraryName + " in the dependencies of module '" +
rootModel.getModule().getName() + "'";
if (MavenConstants.SCOPE_COMPILE.equals(scope) ||
MavenConstants.SCOPE_RUNTIME.equals(scope) ||
MavenConstants.SCOPE_PROVIDEED.equals(scope)) {
final String libraryName = computeLibraryName(mavenProject, depArtifactInfo.getGroupId(),
depArtifactInfo.getArtifactId(),
depArtifactInfo.getVersion());
final boolean mustBeImported = MavenConstants.SCOPE_COMPILE.equals(scope) ||
MavenConstants.SCOPE_RUNTIME.equals(scope) ||
MavenConstants.SCOPE_PROVIDEED.equals(scope);
if (libraryName == null) {
final String message = "Cannot resolve artifact " + depArtifactInfo.getGroupId() + ":" +
depArtifactInfo.getArtifactId();
if (mustBeImported) {
reportError(message, apklibModuleName);
}
else {
LOG.debug(message);
}
continue;
}
final ModifiableRootModel rootModel = rootModelAdapter.getRootModel();
final LibraryOrderEntry libEntry = findLibraryByName(rootModel, libraryName);
if (libEntry == null) {
final String message = "Cannot find library " + libraryName + " in the dependencies of module '" +
rootModel.getModule().getName() + "'";
if (mustBeImported) {
reportError(message, apklibModuleName);
}
else {
LOG.debug(message);
}
continue;
}
final Library library = libEntry.getLibrary();
if (library != null && apklibModuleModel.findLibraryOrderEntry(library) == null) {
final LibraryOrderEntry newLibEntry = apklibModuleModel.addLibraryEntry(library);
newLibEntry.setExported(libEntry.isExported());
newLibEntry.setScope(libEntry.getScope());
}
}
}
@@ -511,6 +525,31 @@ public abstract class AndroidFacetImporterBase extends FacetImporter<AndroidFace
LOG.debug(message);
}
@Nullable
private static String computeLibraryName(@NotNull MavenProject mavenProject,
@Nullable String groupId,
@Nullable String artifactId,
@Nullable String preferredVersion) {
if (artifactId == null) {
return null;
}
String candidate = null;
for (MavenArtifact depArtifact : mavenProject.getDependencies()) {
if (artifactId.equals(depArtifact.getArtifactId()) && Comparing.equal(groupId, depArtifact.getGroupId())) {
final String libName = depArtifact.getLibraryName();
if (Comparing.equal(preferredVersion, depArtifact.getVersion())) {
return libName;
}
else {
candidate = libName;
}
}
}
return candidate;
}
@Nullable
private static LibraryOrderEntry findLibraryByName(ModifiableRootModel model, final String libraryName) {
final Ref<LibraryOrderEntry> result = Ref.create(null);
@@ -588,10 +627,8 @@ public abstract class AndroidFacetImporterBase extends FacetImporter<AndroidFace
new ArrayList<AndroidExternalApklibDependenciesManager.MavenDependencyInfo>();
for (MavenArtifact depArtifact : projectForExternalApklib.getDependencies()) {
final String depArtifactLibraryName = depArtifact.getLibraryName();
dependencies.add(new AndroidExternalApklibDependenciesManager.MavenDependencyInfo(
depArtifact.getMavenId(), depArtifact.getType(), depArtifact.getScope(), depArtifactLibraryName));
depArtifact.getMavenId(), depArtifact.getType(), depArtifact.getScope()));
}
final AndroidExternalApklibDependenciesManager apklibDependenciesManager =