• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ant;
18 
19 import com.android.ant.DependencyHelper.LibraryProcessorFor3rdPartyJars;
20 import com.android.sdklib.SdkConstants;
21 
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.Task;
25 import org.apache.tools.ant.types.Path;
26 import org.apache.tools.ant.types.Path.PathElement;
27 
28 import java.io.File;
29 import java.util.List;
30 
31 public class ComputeProjectClasspathTask extends Task {
32 
33     private String mProjectLocation;
34     private String mProjectClassPathOut;
35 
setProjectLocation(String projectLocation)36     public void setProjectLocation(String projectLocation) {
37         mProjectLocation = projectLocation;
38     }
39 
setProjectClassPathOut(String projectClassPathOut)40     public void setProjectClassPathOut(String projectClassPathOut) {
41         mProjectClassPathOut = projectClassPathOut;
42     }
43 
44     @Override
execute()45     public void execute() throws BuildException {
46         if (mProjectLocation == null) {
47             throw new BuildException("Missing attribute projectLocation");
48         }
49         if (mProjectClassPathOut == null) {
50             throw new BuildException("Missing attribute projectClassPathOut");
51         }
52 
53         DependencyHelper helper = new DependencyHelper(new File(mProjectLocation),
54                 false /*verbose*/);
55 
56         LibraryProcessorFor3rdPartyJars processor = new LibraryProcessorFor3rdPartyJars();
57 
58         helper.processLibraries(processor);
59         List<File> jars = processor.getJars();
60 
61         // add the project's own content of libs/*.jar
62         File libsFolder = new File(mProjectLocation, SdkConstants.FD_NATIVE_LIBS);
63         File[] jarFiles = libsFolder.listFiles(processor.getFilter());
64         if (jarFiles != null) {
65             for (File jarFile : jarFiles) {
66                 jars.add(jarFile);
67             }
68         }
69 
70         jars = helper.sanitizePaths(jars);
71 
72         Project antProject = getProject();
73 
74         System.out.println("Resolved classpath:");
75 
76         // create a path with all the jars and the project's output as well.
77         Path path = new Path(antProject);
78         for (File jar : jars) {
79             PathElement element = path.createPathElement();
80             String p = jar.getAbsolutePath();
81             element.setPath(p);
82             System.out.println(p);
83         }
84 
85         File bin = new File(mProjectLocation,
86                 helper.getOutDir() + File.separator + "classes");
87         PathElement element = path.createPathElement();
88         String p = bin.getAbsolutePath();
89         element.setPath(p);
90         System.out.println(p);
91 
92         antProject.addReference(mProjectClassPathOut, path);
93     }
94 }
95