001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.ant;
016    
017    import aQute.lib.osgi.Analyzer;
018    import aQute.lib.osgi.Constants;
019    
020    import com.liferay.portal.kernel.util.OSDetector;
021    import com.liferay.portal.kernel.util.ReleaseInfo;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    
025    import java.io.File;
026    
027    import java.text.DateFormat;
028    import java.text.SimpleDateFormat;
029    
030    import java.util.Date;
031    import java.util.jar.Attributes;
032    import java.util.jar.Manifest;
033    
034    import org.apache.tools.ant.BuildException;
035    import org.apache.tools.ant.Project;
036    import org.apache.tools.ant.Task;
037    import org.apache.tools.ant.types.Path;
038    import org.apache.tools.ant.types.Reference;
039    
040    /**
041     * @author Raymond Augé
042     */
043    public class ManifestHelperTask extends Task {
044    
045            @Override
046            public void execute() throws BuildException {
047                    try {
048                            doExecute();
049                    }
050                    catch (Exception e) {
051                            throw new BuildException(e);
052                    }
053            }
054    
055            public void setAnalyze(boolean analyze) {
056                    _analyze = analyze;
057            }
058    
059            public void setClasspathRef(Reference reference) {
060                    if (_path == null) {
061                            _path = new Path(getProject());
062                    }
063    
064                    Path path = _path.createPath();
065    
066                    path.setRefid(reference);
067            }
068    
069            protected void doExecute() throws Exception {
070                    Project project = getProject();
071    
072                    project.setProperty("build.revision", getBuildRevision());
073                    project.setProperty("build.time", getDateString(new Date()));
074                    project.setProperty(
075                            "release.info.build.date",
076                            String.valueOf(ReleaseInfo.getBuildDate()));
077                    project.setProperty(
078                            "release.info.build.number",
079                            String.valueOf(ReleaseInfo.getBuildNumber()));
080                    project.setProperty(
081                            "release.info.code.name", ReleaseInfo.getCodeName());
082                    project.setProperty(
083                            "release.info.parent.build.number",
084                            String.valueOf(ReleaseInfo.getParentBuildNumber()));
085                    project.setProperty(
086                            "release.info.release.info", ReleaseInfo.getReleaseInfo());
087                    project.setProperty(
088                            "release.info.server.info", ReleaseInfo.getServerInfo());
089                    project.setProperty("release.info.vendor", ReleaseInfo.getVendor());
090                    project.setProperty("release.info.version", ReleaseInfo.getVersion());
091    
092                    if (!_analyze) {
093                            return;
094                    }
095    
096                    Analyzer analyzer = new Analyzer();
097    
098                    analyzer.setBase(project.getBaseDir());
099    
100                    File classesDir = new File(project.getBaseDir(), "classes");
101    
102                    analyzer.setJar(classesDir);
103    
104                    File file = new File(project.getBaseDir(), "bnd.bnd");
105    
106                    if (file.exists()) {
107                            analyzer.setProperties(file);
108                    }
109                    else {
110                            analyzer.setProperty(
111                                    Constants.IMPORT_PACKAGE, "*;resolution:=optional");
112                    }
113    
114                    Manifest manifest = analyzer.calcManifest();
115    
116                    Attributes attributes = manifest.getMainAttributes();
117    
118                    project.setProperty(
119                            "export.packages", attributes.getValue(Constants.EXPORT_PACKAGE));
120                    project.setProperty(
121                            "import.packages", attributes.getValue(Constants.IMPORT_PACKAGE));
122            }
123    
124            protected String execute(String command) throws Exception {
125                    Runtime runtime = Runtime.getRuntime();
126    
127                    Process process = runtime.exec(command);
128    
129                    return StringUtil.read(process.getInputStream());
130            }
131    
132            protected String getBuildRevision() throws Exception {
133                    Project project = getProject();
134    
135                    File projectDir = new File(
136                            project.getBaseDir(), project.getProperty("project.dir"));
137    
138                    File gitDir = new File(projectDir, ".git");
139    
140                    if (gitDir.exists()) {
141                            if (OSDetector.isWindows()) {
142                                    return execute("cmd /c git rev-parse HEAD");
143                            }
144                            else {
145                                    return execute("git rev-parse HEAD");
146                            }
147                    }
148    
149                    File svnDir = new File(projectDir, ".svn");
150    
151                    if (svnDir.exists()) {
152                            if (OSDetector.isWindows()) {
153                                    return execute("cmd /c svnversion .");
154                            }
155                            else {
156                                    return execute("svnversion .");
157                            }
158                    }
159    
160                    return StringPool.BLANK;
161            }
162    
163            protected String getDateString(Date date) {
164                    DateFormat dateFormat = new SimpleDateFormat(_PATTERN);
165    
166                    return dateFormat.format(date);
167            }
168    
169            private static final String _PATTERN = "EEE MMM d HH:mm:ss z yyyy";
170    
171            private boolean _analyze;
172            private Path _path;
173    
174    }