diff --git a/plugins/com.google.cloud.tools.eclipse.appengine.libraries.test/src/com/google/cloud/tools/eclipse/appengine/libraries/repository/MavenHelpTest.java b/plugins/com.google.cloud.tools.eclipse.appengine.libraries.test/src/com/google/cloud/tools/eclipse/appengine/libraries/repository/MavenHelpTest.java deleted file mode 100644 index 5e88aef107..0000000000 --- a/plugins/com.google.cloud.tools.eclipse.appengine.libraries.test/src/com/google/cloud/tools/eclipse/appengine/libraries/repository/MavenHelpTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2017 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.cloud.tools.eclipse.appengine.libraries.repository; - -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; - -import com.google.cloud.tools.eclipse.appengine.libraries.model.MavenCoordinates; -import org.eclipse.core.runtime.IPath; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; - -@RunWith(MockitoJUnitRunner.class) -public class MavenHelpTest { - - private static final String EXPECTED_DOWNLOAD_FOLDER = - ".metadata/.plugins/com.google.cloud.tools.eclipse.appengine.libraries/downloads/groupId/artifactId/1.0.0"; - - @Mock private MavenCoordinates artifact; - - @Test - public void testBundleStateBasedMavenFolder_withSpecificVersion() { - when(artifact.getGroupId()).thenReturn("groupId"); - when(artifact.getArtifactId()).thenReturn("artifactId"); - when(artifact.getVersion()).thenReturn("1.0.0"); - IPath folder = MavenHelper.bundleStateBasedMavenFolder(artifact); - assertTrue(folder.toString().endsWith(EXPECTED_DOWNLOAD_FOLDER)); - } - - @Test(expected = IllegalArgumentException.class) - public void testBundleStateBasedMavenFolder_withLatestVersion() { - when(artifact.getVersion()).thenReturn("LATEST"); - MavenHelper.bundleStateBasedMavenFolder(artifact); - } -} diff --git a/plugins/com.google.cloud.tools.eclipse.appengine.libraries.test/src/com/google/cloud/tools/eclipse/appengine/libraries/repository/MavenHelperTest.java b/plugins/com.google.cloud.tools.eclipse.appengine.libraries.test/src/com/google/cloud/tools/eclipse/appengine/libraries/repository/MavenHelperTest.java new file mode 100644 index 0000000000..d4771edee1 --- /dev/null +++ b/plugins/com.google.cloud.tools.eclipse.appengine.libraries.test/src/com/google/cloud/tools/eclipse/appengine/libraries/repository/MavenHelperTest.java @@ -0,0 +1,103 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.tools.eclipse.appengine.libraries.repository; + +import static org.junit.Assert.assertTrue; + +import com.google.cloud.tools.eclipse.appengine.libraries.model.MavenCoordinates; +import org.apache.maven.artifact.Artifact; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.junit.Assert; +import org.junit.Test; + +public class MavenHelperTest { + + private static final String EXPECTED_DOWNLOAD_FOLDER = + ".metadata/.plugins/com.google.cloud.tools.eclipse.appengine.libraries/downloads/groupId/artifactId/1.0.0"; + + @Test + public void testBundleStateBasedMavenFolder_withSpecificVersion() { + MavenCoordinates coordinates = new MavenCoordinates.Builder() + .setGroupId("groupId") + .setArtifactId("artifactId") + .setVersion("1.0.0") + .build(); + IPath folder = MavenHelper.bundleStateBasedMavenFolder(coordinates); + assertTrue(folder.toString().endsWith(EXPECTED_DOWNLOAD_FOLDER)); + } + + @Test + public void testResolveArtifact() throws CoreException { + MavenCoordinates coordinates = new MavenCoordinates.Builder() + .setGroupId("com.google.cloud") + .setArtifactId("google-cloud-datastore") + .setVersion("1.102.1") + .build(); + Artifact artifact = MavenHelper.resolveArtifact(coordinates, new NullProgressMonitor()); + Assert.assertEquals("google-cloud-datastore", artifact.getArtifactId()); + } + + @Test + public void testResolveArtifact_googleApiClient() throws CoreException { + MavenCoordinates coordinates = new MavenCoordinates.Builder() + .setGroupId("com.google.api-client") + .setArtifactId("google-api-client") + .setVersion("1.30.8") + .build(); + Artifact artifact = MavenHelper.resolveArtifact(coordinates, new NullProgressMonitor()); + Assert.assertEquals("google-api-client", artifact.getArtifactId()); + } + + @Test + public void testResolveArtifact_android() throws CoreException { + MavenCoordinates coordinates = new MavenCoordinates.Builder() + .setGroupId("androidx.annotation") + .setArtifactId("annotation") + .setVersion("1.1.0") + .setRepository("https://maven.google.com") + .build(); + Artifact artifact = MavenHelper.resolveArtifact(coordinates, new NullProgressMonitor()); + Assert.assertEquals("annotation", artifact.getArtifactId()); + } + + @Test + public void testResolveArtifact_android_noRepository() throws CoreException { + MavenCoordinates coordinates = new MavenCoordinates.Builder() + .setGroupId("androidx.annotation") + .setArtifactId("annotation") + .setVersion("1.1.0") + .build(); + Artifact artifact = MavenHelper.resolveArtifact(coordinates, new NullProgressMonitor()); + Assert.assertEquals("annotation", artifact.getArtifactId()); + } + + @Test + public void testBundleStateBasedMavenFolder_withLatestVersion() { + MavenCoordinates coordinates = new MavenCoordinates.Builder() + .setGroupId("com.google.cloud") + .setArtifactId("datastore") + .setVersion("LATEST") + .build(); + try { + MavenHelper.bundleStateBasedMavenFolder(coordinates); + Assert.fail(); + } catch (IllegalArgumentException expected) { + } + } +} diff --git a/plugins/com.google.cloud.tools.eclipse.appengine.libraries/src/com/google/cloud/tools/eclipse/appengine/libraries/model/MavenCoordinates.java b/plugins/com.google.cloud.tools.eclipse.appengine.libraries/src/com/google/cloud/tools/eclipse/appengine/libraries/model/MavenCoordinates.java index 32d284fe48..5f40718d4e 100644 --- a/plugins/com.google.cloud.tools.eclipse.appengine.libraries/src/com/google/cloud/tools/eclipse/appengine/libraries/model/MavenCoordinates.java +++ b/plugins/com.google.cloud.tools.eclipse.appengine.libraries/src/com/google/cloud/tools/eclipse/appengine/libraries/model/MavenCoordinates.java @@ -138,6 +138,9 @@ public Builder setGroupId(String groupId) { Preconditions.checkNotNull(groupId, "groupId null"); Preconditions.checkArgument(!groupId.isEmpty(), "groupId is empty"); this.groupId = groupId; + if (groupId.startsWith("androidx")) { + setRepository("https://maven.google.com"); + } return this; } diff --git a/plugins/com.google.cloud.tools.eclipse.appengine.libraries/src/com/google/cloud/tools/eclipse/appengine/libraries/repository/MavenHelper.java b/plugins/com.google.cloud.tools.eclipse.appengine.libraries/src/com/google/cloud/tools/eclipse/appengine/libraries/repository/MavenHelper.java index 69afc1b401..bfc191bd1f 100644 --- a/plugins/com.google.cloud.tools.eclipse.appengine.libraries/src/com/google/cloud/tools/eclipse/appengine/libraries/repository/MavenHelper.java +++ b/plugins/com.google.cloud.tools.eclipse.appengine.libraries/src/com/google/cloud/tools/eclipse/appengine/libraries/repository/MavenHelper.java @@ -54,14 +54,14 @@ public static Artifact resolveArtifact( private static List getRepository(MavenCoordinates mavenCoordinates) throws CoreException { if (MavenCoordinates.MAVEN_CENTRAL_REPO.equals(mavenCoordinates.getRepository())) { - // M2Eclipse will use the Maven Central repo in case null is used + // M2Eclipse uses the Maven Central repo if the repsoitory list is null return null; } else { - return Collections.singletonList(getCustomRepository(mavenCoordinates.getRepository())); + return Collections.singletonList(makeRepository(mavenCoordinates.getRepository())); } } - private static ArtifactRepository getCustomRepository(String repository) throws CoreException { + private static ArtifactRepository makeRepository(String repository) throws CoreException { try { URI repoUri = new URI(repository); if (!repoUri.isAbsolute()) { diff --git a/plugins/com.google.cloud.tools.eclipse.util/src/com/google/cloud/tools/eclipse/util/MavenUtils.java b/plugins/com.google.cloud.tools.eclipse.util/src/com/google/cloud/tools/eclipse/util/MavenUtils.java index 25522118b9..5c0cab5fa0 100644 --- a/plugins/com.google.cloud.tools.eclipse.util/src/com/google/cloud/tools/eclipse/util/MavenUtils.java +++ b/plugins/com.google.cloud.tools.eclipse.util/src/com/google/cloud/tools/eclipse/util/MavenUtils.java @@ -39,6 +39,7 @@ import org.eclipse.core.runtime.jobs.ISchedulingRule; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.m2e.core.MavenPlugin; +import org.eclipse.m2e.core.embedder.IMaven; import org.eclipse.m2e.core.embedder.IMavenExecutionContext; import org.eclipse.m2e.core.internal.MavenPluginActivator; import org.w3c.dom.DOMException; @@ -93,9 +94,9 @@ public static Artifact resolveArtifact( return runOperation( monitor, (context, system, progress) -> { - Artifact artifact = - MavenPlugin.getMaven() - .resolve(groupId, artifactId, version, type, classifier, repositories, progress); + IMaven maven = MavenPlugin.getMaven(); + Artifact artifact = maven.resolve( + groupId, artifactId, version, type, classifier, repositories, progress); return artifact; }); }