Home | History | Annotate | Download | only in build
      1 /*
      2  * Copyright (C) 2008 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 util.build;
     18 
     19 import sun.tools.jar.Main;
     20 
     21 import java.io.File;
     22 import java.io.IOException;
     23 
     24 
     25 public class JarBuildStep extends BuildStep {
     26 
     27     String destFileName;
     28     private final boolean deleteInputFileAfterBuild;
     29 
     30     public JarBuildStep(BuildFile inputFile, String destFileName,
     31             BuildFile outputFile, boolean deleteInputFileAfterBuild) {
     32         super(inputFile, outputFile);
     33         this.destFileName = destFileName;
     34         this.deleteInputFileAfterBuild = deleteInputFileAfterBuild;
     35     }
     36 
     37     @Override
     38     boolean build() {
     39         if (super.build()) {
     40             File tempFile = new File(inputFile.folder, destFileName);
     41             try {
     42                 if (!inputFile.fileName.equals(tempFile)) {
     43                     copyFile(inputFile.fileName, tempFile);
     44                 } else {
     45                     tempFile = null;
     46                 }
     47             } catch (IOException e) {
     48                 System.err.println("io exception:"+e.getMessage());
     49                 e.printStackTrace();
     50                 return false;
     51             }
     52 
     53             File outDir = outputFile.fileName.getParentFile();
     54             if (!outDir.exists() && !outDir.mkdirs()) {
     55                 System.err.println("failed to create output dir: "
     56                         + outDir.getAbsolutePath());
     57                 return false;
     58             }
     59             String[] arguments = new String[] {
     60                     "-cMf", outputFile.fileName.getAbsolutePath(), "-C",
     61                     inputFile.folder.getAbsolutePath(), destFileName};
     62             Main main = new Main(System.out, System.err, "jar");
     63             boolean success = main.run(arguments);
     64 
     65             if (success) {
     66                 if (tempFile != null) {
     67                     tempFile.delete();
     68                 }
     69                 if (deleteInputFileAfterBuild) {
     70                     inputFile.fileName.delete();
     71                 }
     72             } else {
     73                 System.err.println("exception in JarBuildStep while calling jar with args:" +
     74                         " \"-cMf\", "+outputFile.fileName.getAbsolutePath()+", \"-C\"," +
     75                         inputFile.folder.getAbsolutePath()+", "+ destFileName);
     76             }
     77             return success;
     78         }
     79         return false;
     80     }
     81 
     82     @Override
     83     public int hashCode() {
     84         return inputFile.hashCode() ^ outputFile.hashCode()
     85                 ^ destFileName.hashCode();
     86     }
     87 
     88     @Override
     89     public boolean equals(Object obj) {
     90         if (super.equals(obj)) {
     91             JarBuildStep other = (JarBuildStep) obj;
     92             return inputFile.equals(other.inputFile)
     93                     && outputFile.equals(other.outputFile)
     94                     && destFileName.equals(other.destFileName);
     95 
     96         }
     97         return false;
     98     }
     99 
    100 }
    101