001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.jlink;
019:
020: import java.io.File;
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Project;
023: import org.apache.tools.ant.taskdefs.MatchingTask;
024: import org.apache.tools.ant.types.Path;
025:
026: /**
027: * This class defines objects that can link together various jar and
028: * zip files.
029: *
030: * <p>It is basically a wrapper for the jlink code written originally
031: * by <a href="mailto:beard@netscape.com">Patrick Beard</a>. The
032: * classes org.apache.tools.ant.taskdefs.optional.jlink.Jlink and
033: * org.apache.tools.ant.taskdefs.optional.jlink.ClassNameReader
034: * support this class.</p>
035: *
036: * <p>For example:
037: * <code>
038: * <pre>
039: * <jlink compress="false" outfile="out.jar"/>
040: * <mergefiles>
041: * <pathelement path="${build.dir}/mergefoo.jar"/>
042: * <pathelement path="${build.dir}/mergebar.jar"/>
043: * </mergefiles>
044: * <addfiles>
045: * <pathelement path="${build.dir}/mac.jar"/>
046: * <pathelement path="${build.dir}/pc.zip"/>
047: * </addfiles>
048: * </jlink>
049: * </pre>
050: * </code>
051: *
052: * @ant.task ignore="true"
053: */
054: public class JlinkTask extends MatchingTask {
055:
056: /**
057: * The output file for this run of jlink. Usually a jar or zip file.
058: * @param outfile the output file
059: */
060: public void setOutfile(File outfile) {
061: this .outfile = outfile;
062: }
063:
064: /**
065: * Establishes the object that contains the files to
066: * be merged into the output.
067: * @return a path to be configured
068: */
069: public Path createMergefiles() {
070: if (this .mergefiles == null) {
071: this .mergefiles = new Path(getProject());
072: }
073: return this .mergefiles.createPath();
074: }
075:
076: /**
077: * Sets the files to be merged into the output.
078: * @param mergefiles a path
079: */
080: public void setMergefiles(Path mergefiles) {
081: if (this .mergefiles == null) {
082: this .mergefiles = mergefiles;
083: } else {
084: this .mergefiles.append(mergefiles);
085: }
086: }
087:
088: /**
089: * Establishes the object that contains the files to
090: * be added to the output.
091: * @return a path to be configured
092: */
093: public Path createAddfiles() {
094: if (this .addfiles == null) {
095: this .addfiles = new Path(getProject());
096: }
097: return this .addfiles.createPath();
098: }
099:
100: /**
101: * Sets the files to be added into the output.
102: * @param addfiles a path
103: */
104: public void setAddfiles(Path addfiles) {
105: if (this .addfiles == null) {
106: this .addfiles = addfiles;
107: } else {
108: this .addfiles.append(addfiles);
109: }
110: }
111:
112: /**
113: * Defines whether or not the output should be compacted.
114: * @param compress a <code>boolean</code> value
115: */
116: public void setCompress(boolean compress) {
117: this .compress = compress;
118: }
119:
120: /**
121: * Does the adding and merging.
122: * @throws BuildException on error
123: */
124: public void execute() throws BuildException {
125: //Be sure everything has been set.
126: if (outfile == null) {
127: throw new BuildException("outfile attribute is required! "
128: + "Please set.");
129: }
130: if (!haveAddFiles() && !haveMergeFiles()) {
131: throw new BuildException(
132: "addfiles or mergefiles required! " + "Please set.");
133: }
134: log("linking: " + outfile.getPath());
135: log("compression: " + compress, Project.MSG_VERBOSE);
136: jlink linker = new jlink();
137: linker.setOutfile(outfile.getPath());
138: linker.setCompression(compress);
139: if (haveMergeFiles()) {
140: log("merge files: " + mergefiles.toString(),
141: Project.MSG_VERBOSE);
142: linker.addMergeFiles(mergefiles.list());
143: }
144: if (haveAddFiles()) {
145: log("add files: " + addfiles.toString(),
146: Project.MSG_VERBOSE);
147: linker.addAddFiles(addfiles.list());
148: }
149: try {
150: linker.link();
151: } catch (Exception ex) {
152: throw new BuildException(ex, getLocation());
153: }
154: }
155:
156: private boolean haveAddFiles() {
157: return haveEntries(addfiles);
158: }
159:
160: private boolean haveMergeFiles() {
161: return haveEntries(mergefiles);
162: }
163:
164: private boolean haveEntries(Path p) {
165: if (p == null) {
166: return false;
167: }
168: if (p.size() > 0) {
169: return true;
170: }
171: return false;
172: }
173:
174: private File outfile = null;
175:
176: private Path mergefiles = null;
177:
178: private Path addfiles = null;
179:
180: private boolean compress = false;
181:
182: }
|