Skip to content

Commit

Permalink
Perform all logic around choosing SDK path & platform, in one place i…
Browse files Browse the repository at this point in the history
…n AbstractAndroidMojo.

  Improved javadoc for AbstractAndroidMojo#getAndroidSdk.
  Inserted environment variable ANDROID_HOME lookup there too.
  Moved ANDROID_HOME test accordingly.
  • Loading branch information
hugojosefson committed Feb 1, 2010
1 parent 332e91e commit 0590187
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 38 deletions.
8 changes: 7 additions & 1 deletion pom.xml
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2009 Jayway AB
Copyright (C) 2009, 2010 Jayway AB
Copyright (C) 2007-2008 JVending Masa
Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -75,6 +75,12 @@
<version>4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-reflect</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009 Jayway AB
* Copyright (C) 2009, 2010 Jayway AB
* Copyright (C) 2007-2008 JVending Masa
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -37,6 +37,8 @@
import java.net.URL;
import java.util.*;

import static org.apache.commons.lang.StringUtils.isBlank;

/**
* Contains common fields and methods for android mojos.
*
Expand Down Expand Up @@ -227,6 +229,11 @@ public abstract class AbstractAndroidMojo extends AbstractMojo {
*/
private String envANDROID_HOME;

/**
* The <code>ANDROID_HOME</code> environment variable name.
*/
public static final String ENV_ANDROID_HOME = "ANDROID_HOME";

/**
* <p>Parameter designed to pick up <code>-Dandroid.sdk.platform</code> in case there is no pom with an
* <code>&lt;sdk&gt;</code> configuration tag.</p>
Expand Down Expand Up @@ -578,18 +585,60 @@ protected String[] findFilesInDirectory(File baseDirectory, String... includes)
/**
* <p>Returns the Android SDK to use.</p>
*
* <p>Current implementation looks for <code>&lt;sdk&gt;&lt;path&gt;</code> configuration in pom, then System
* property <code>android.sdk.path</code>, then environment variable <code>ANDROID_HOME</code>.
*
* <p>This is where we collect all logic for how to lookup where it is, and which one to choose. The lookup is
* based on available parameters. This method should be the only one you should need to look at to understand how
* the Android SDK is chosen, and from where on disk.</p>
*
*
* @return the Android SDK to use.
*/
protected AndroidSdk getAndroidSdk(){
if (sdk==null){
return new AndroidSdk(sdkPath, sdkPlatform);
}else{
return new AndroidSdk(sdk.getPath(), sdk.getPlatform());
File chosenSdkPath;
String chosenSdkPlatform;

if (sdk != null) {
// An <sdk> tag exists in the pom.

if (sdk.getPath() != null){
// An <sdk><path> tag is set in the pom.

chosenSdkPath = sdk.getPath();
}else{
// There is no <sdk><path> tag in the pom.

if (sdkPath != null){
// -Dandroid.sdk.path is set on command line, or via <properties><sdk.path>...
chosenSdkPath = sdkPath;
}else{
// No -Dandroid.sdk.path is set on command line, or via <properties><sdk.path>...
chosenSdkPath = new File(System.getenv(ENV_ANDROID_HOME));
}
}

// Use <sdk><platform> from pom if it's there, otherwise try -Dandroid.sdk.platform from command line or <properties><sdk.platform>...
if (!isBlank(sdk.getPlatform())){
chosenSdkPlatform = sdk.getPlatform();
}else{
chosenSdkPlatform = sdkPlatform;
}
} else {
// There is no <sdk> tag in the pom.

if (sdkPath != null){
// -Dandroid.sdk.path is set on command line, or via <properties><sdk.path>...
chosenSdkPath = sdkPath;
}else{
// No -Dandroid.sdk.path is set on command line, or via <properties><sdk.path>...
chosenSdkPath = new File(System.getenv(ENV_ANDROID_HOME));
}

// Use any -Dandroid.sdk.platform from command line or <properties><sdk.platform>...
chosenSdkPlatform = sdkPlatform;
}

return new AndroidSdk(chosenSdkPath, chosenSdkPlatform);
}

}
25 changes: 3 additions & 22 deletions src/main/java/com/jayway/maven/plugins/android/AndroidSdk.java
Expand Up @@ -30,23 +30,12 @@
* @author hugo.josefson@jayway.com
*/
public class AndroidSdk {
private final File path ;
private final File path;
private final String platform;
private static final String PARAMETER_MESSAGE = "Please provide a proper Android SDK directory path as configuration parameter <sdk><path>...</path></sdk> in the plugin <configuration/>. As an alternative, you may add the parameter to commandline: -Dandroid.sdk.path=...";
private static final String PARAMETER_MESSAGE = "Please provide a proper Android SDK directory path as configuration parameter <sdk><path>...</path></sdk> in the plugin <configuration/>. As an alternative, you may add the parameter to commandline: -Dandroid.sdk.path=... or set environment variable " + AbstractAndroidMojo.ENV_ANDROID_HOME + ".";

public static final String ENV_ANDROID_HOME = "ANDROID_HOME";
public AndroidSdk(File path, String platform) {

// defaulting to ANDROID_HOME so that setting is not necessary in pom.xml
if (path == null || path.toString() =="")
{
this.path = new File(System.getenv(ENV_ANDROID_HOME));
}
else
{
this.path = path;
}

this.path = path;
this.platform = platform;
}

Expand Down Expand Up @@ -173,12 +162,4 @@ public File getPlatform() {
}
}

/**
* Get the path to the sdk.
* @return File object with path
*/
File getPath()
{
return this.path;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009 Jayway AB
* Copyright (C) 2009, 2010 Jayway AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.fest.reflect.core.Reflection;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -34,13 +35,22 @@
* @author hugo.josefson@jayway.com
*/
public class AbstractAndroidMojoTest {
protected MyAbstractAndroidMojo androidMojo;
protected AbstractAndroidMojo androidMojo;

@Before
public void setUp() throws Exception {
androidMojo = new MyAbstractAndroidMojo();
}

@Test
public void givenNoPathUseAndroidHomePath()
{
SdkTestSupport testSupport = new SdkTestSupport();
AndroidSdk sdk = androidMojo.getAndroidSdk();
File path = Reflection.field("path").ofType(File.class).in(sdk).get();
Assert.assertEquals(testSupport.getEnv_ANDROID_HOME(), path.getAbsolutePath());
}

@Test
public void givenAndroidManifestThenTargetPackageIsFound() throws MalformedURLException, URISyntaxException, MojoExecutionException {
final URL url = this.getClass().getResource("AndroidManifest.xml");
Expand Down
Expand Up @@ -37,13 +37,6 @@ public void setUp(){
sdkTestSupport = new SdkTestSupport();
}

@Test
public void givenNoPathUseAndroidHomePath()
{
AndroidSdk sdk = new AndroidSdk(new File(""), "1.5");
Assert.assertEquals(System.getenv("ANDROID_HOME"), sdk.getPath().getAbsolutePath());
}

@Test
public void givenToolAdbThenPathIsCommon() {
final String pathForTool =sdkTestSupport.getSdk_with_platform_1_5().getPathForTool("adb");
Expand Down

0 comments on commit 0590187

Please sign in to comment.