View Javadoc
1   /*
2    * Copyright (C) 2009 Jayway AB
3    * Copyright (C) 2007-2008 JVending Masa
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package com.simpligility.maven.plugins.android.standalonemojos;
18  
19  import com.simpligility.maven.plugins.android.AbstractAndroidMojo;
20  import com.simpligility.maven.plugins.android.config.ConfigHandler;
21  import com.simpligility.maven.plugins.android.config.ConfigPojo;
22  import com.simpligility.maven.plugins.android.config.PullParameter;
23  import com.simpligility.maven.plugins.android.configuration.DeployApk;
24  import com.simpligility.maven.plugins.android.configuration.ValidationResponse;
25  
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  
32  import java.io.File;
33  
34  /**
35   * Undeploys a specified Android application apk from attached devices and emulators. 
36   * By default it will undeploy from all, but a subset or single one can be configured 
37   * with the device and devices parameters. You can supply the package of the 
38   * application and/or an apk file. This goal can be used in non-android projects and as 
39   * standalone execution on the command line.<br>
40   *
41   * @author Manfred Moser - manfred@simpligility.com
42   */
43  @Mojo( name = "undeploy-apk", requiresProject = false )
44  public class UndeployApkMojo extends AbstractAndroidMojo
45  {
46      /**
47       * Configuration for apk file undeployment within a pom file. See {@link #deployapkFilename}. 
48       * 
49       * <pre>
50       * &lt;deployapk&gt;
51       *    &lt;filename&gt;yourapk.apk&lt;/filename&gt;
52       *    &lt;packagename&gt;com.yourcompany.app&lt;/packagename&gt;
53       * &lt;/deployapk&gt;
54       * </pre>
55       */
56      @ConfigPojo
57      @Parameter
58      protected DeployApk deployapk;
59  
60      @Parameter( property = "android.deployapk.filename" )
61      private File deployapkFilename;
62  
63      @PullParameter
64      private File parsedFilename;
65  
66      @Parameter( property = "android.deployapk.packagename" )
67      private String deployapkPackagename;
68  
69      @PullParameter
70      private String parsedPackagename;
71  
72      /**
73       * @throws MojoExecutionException
74       * @throws MojoFailureException
75       */
76      public void execute() throws MojoExecutionException, MojoFailureException
77      {
78          ConfigHandler configHandler = new ConfigHandler( this, this.session, this.execution );
79          configHandler.parseConfiguration();
80  
81          if ( parsedFilename == null && parsedPackagename == null )
82          {
83              throw new MojoFailureException( "\n\n One of the parameters android.deployapk.packagename "
84                      + "or android.deployapk.filename is required. \n" );
85          }
86          
87          if ( StringUtils.isNotBlank( parsedPackagename ) )
88          {
89              getLog().debug( "Undeploying with packagename " + parsedPackagename );
90              undeployApk( parsedPackagename );
91          }
92  
93          ValidationResponse response = DeployApk.validFileParameter( parsedFilename );
94          if ( response.isValid() ) 
95          {
96              getLog().debug( "Undeploying with file " + parsedFilename );
97              undeployApk( parsedFilename );
98          } 
99          else 
100         {
101             getLog().info( "Ignoring invalid file parameter." );
102             getLog().debug( response.getMessage() );
103             
104         }
105     }
106 }