update copyright
[fedora-idea.git] / plugins / devkit / src / build / ant / BuildJarTarget.java
blob8c3083e5692c59920b756b361b9a43c99fbf1f28
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * User: anna
19 * Date: 19-Dec-2006
21 package org.jetbrains.idea.devkit.build.ant;
23 import com.intellij.compiler.ant.*;
24 import com.intellij.compiler.ant.taskdefs.*;
25 import com.intellij.openapi.module.Module;
26 import com.intellij.openapi.roots.CompilerModuleExtension;
27 import com.intellij.openapi.roots.OrderRootType;
28 import com.intellij.openapi.roots.libraries.Library;
29 import com.intellij.openapi.vfs.JarFileSystem;
30 import com.intellij.openapi.vfs.VfsUtil;
31 import com.intellij.openapi.vfs.VirtualFile;
32 import org.jetbrains.annotations.NonNls;
33 import org.jetbrains.idea.devkit.DevKitBundle;
34 import org.jetbrains.idea.devkit.build.PluginBuildConfiguration;
35 import org.jetbrains.idea.devkit.build.PluginBuildUtil;
36 import org.jetbrains.idea.devkit.build.PrepareToDeployAction;
38 import java.io.File;
39 import java.io.IOException;
40 import java.util.HashSet;
42 public class BuildJarTarget extends Target {
43 public BuildJarTarget(final ModuleChunk chunk,
44 final GenerationOptions genOptions,
45 final PluginBuildConfiguration moduleBuildProperties) {
46 super(PluginBuildProperties.getBuildJarTargetName(chunk.getName()), null, DevKitBundle.message("ant.build.jar.description", chunk.getName()), null);
48 final File moduleBaseDir = chunk.getBaseDir();
50 final Module[] modules = chunk.getModules();
52 final Module module = modules[0];
54 final String moduleName = module.getName();
57 final HashSet<Library> libs = new HashSet<Library>();
58 for (Module module1 : modules) {
59 PluginBuildUtil.getLibraries(module1, libs);
62 final String jarPathPropertyRef = BuildProperties.propertyRef(PluginBuildProperties.getJarPathProperty(moduleName));
64 if (libs.isEmpty()) {
65 add(createPluginsJar(jarPathPropertyRef, modules, moduleBaseDir, genOptions, moduleBuildProperties));
66 } else {
67 @NonNls final String tempSuffix = "temp";
68 final File jarDir = new File(moduleBaseDir.getParentFile(), tempSuffix);
69 String tempDir = GenerationUtils.toRelativePath(jarDir.getPath(), chunk, genOptions);
70 final String tempDirProperty = BuildProperties.getTempDirForModuleProperty(moduleName);
72 add(new Property(tempDirProperty, tempDir));
73 add(new Mkdir(BuildProperties.propertyRef(tempDirProperty)));
75 add(new Mkdir(BuildProperties.propertyRef(tempDirProperty) + "/lib"));
77 final @NonNls String libRelativePath = BuildProperties.propertyRef(tempDirProperty) + "/lib/";
79 add(createPluginsJar(libRelativePath + chunk.getName() + ".jar", modules, moduleBaseDir, genOptions, moduleBuildProperties));
81 for (Library lib : libs) {
82 final VirtualFile[] files = lib.getFiles(OrderRootType.CLASSES);
83 for (VirtualFile file : files) {
84 final String relativePath = GenerationUtils.toRelativePath(file, chunk, genOptions);
85 if (file.getFileSystem() instanceof JarFileSystem) {
86 add(new Copy(relativePath, libRelativePath + file.getName()));
87 } else {
88 final Jar jar = new Jar(libRelativePath + file.getNameWithoutExtension() + ".jar", "preserve");
89 jar.add(new ZipFileSet(relativePath, "", true));
90 add(jar);
95 final Tag zipTag = new Zip(jarPathPropertyRef);
96 zipTag.add(new FileSet(tempDir));
97 add(zipTag);
99 add(new Delete(BuildProperties.propertyRef(tempDirProperty)));
103 private static Tag createPluginsJar(@NonNls final String jarPathProperty,
104 final Module[] modules, final File moduleBaseDir,
105 final GenerationOptions genOptions,
106 final PluginBuildConfiguration moduleBuildProperties) {
107 final Tag jarTag = new Jar(jarPathProperty, "preserve");
108 for (Module m : modules) {
109 final String path = VfsUtil.urlToPath(CompilerModuleExtension.getInstance(m).getCompilerOutputUrl());
110 final String relativePath = GenerationUtils.toRelativePath(path, moduleBaseDir, m, genOptions);
111 jarTag.add(new ZipFileSet(relativePath, "", true));
113 final Module module = modules[0];
115 //plugin.xml
117 final String pluginXmlPath = moduleBuildProperties.getPluginXmlPath();
118 final String relativePluginXMLPath = GenerationUtils.toRelativePath(pluginXmlPath, moduleBaseDir, module, genOptions);
119 jarTag.add(new ZipFileSet(relativePluginXMLPath, "META-INF/plugin.xml", false));
121 //manifest
122 final Manifest manifestTag = new Manifest();
123 jarTag.add(manifestTag);
124 final java.util.jar.Manifest manifest;
125 try {
126 manifest = PrepareToDeployAction.createOrFindManifest(moduleBuildProperties);
128 catch (IOException e) {
129 return jarTag;
131 manifestTag.applyAttributes(manifest);
132 return jarTag;