1 | /*
|
---|
2 | * Copyright 2000,2002,2004 The Apache Software Foundation
|
---|
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 org.apache.tools.ant.taskdefs.optional.jlink;
|
---|
18 |
|
---|
19 | import java.io.File;
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.Project;
|
---|
22 | import org.apache.tools.ant.taskdefs.MatchingTask;
|
---|
23 | import org.apache.tools.ant.types.Path;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * This class defines objects that can link together various jar and
|
---|
27 | * zip files.
|
---|
28 | *
|
---|
29 | * <p>It is basically a wrapper for the jlink code written originally
|
---|
30 | * by <a href="mailto:[email protected]">Patrick Beard</a>. The
|
---|
31 | * classes org.apache.tools.ant.taskdefs.optional.jlink.Jlink and
|
---|
32 | * org.apache.tools.ant.taskdefs.optional.jlink.ClassNameReader
|
---|
33 | * support this class.</p>
|
---|
34 | *
|
---|
35 | * <p>For example:
|
---|
36 | * <code>
|
---|
37 | * <pre>
|
---|
38 | * <jlink compress="false" outfile="out.jar"/>
|
---|
39 | * <mergefiles>
|
---|
40 | * <pathelement path="${build.dir}/mergefoo.jar"/>
|
---|
41 | * <pathelement path="${build.dir}/mergebar.jar"/>
|
---|
42 | * </mergefiles>
|
---|
43 | * <addfiles>
|
---|
44 | * <pathelement path="${build.dir}/mac.jar"/>
|
---|
45 | * <pathelement path="${build.dir}/pc.zip"/>
|
---|
46 | * </addfiles>
|
---|
47 | * </jlink>
|
---|
48 | * </pre>
|
---|
49 | * </code>
|
---|
50 | *
|
---|
51 | * @ant.task ignore="true"
|
---|
52 | */
|
---|
53 | public class JlinkTask extends MatchingTask {
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * The output file for this run of jlink. Usually a jar or zip file.
|
---|
57 | */
|
---|
58 | public void setOutfile(File outfile) {
|
---|
59 | this.outfile = outfile;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Establishes the object that contains the files to
|
---|
64 | * be merged into the output.
|
---|
65 | */
|
---|
66 | public Path createMergefiles() {
|
---|
67 | if (this.mergefiles == null) {
|
---|
68 | this.mergefiles = new Path(getProject());
|
---|
69 | }
|
---|
70 | return this.mergefiles.createPath();
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Sets the files to be merged into the output.
|
---|
75 | */
|
---|
76 | public void setMergefiles(Path mergefiles) {
|
---|
77 | if (this.mergefiles == null) {
|
---|
78 | this.mergefiles = mergefiles;
|
---|
79 | } else {
|
---|
80 | this.mergefiles.append(mergefiles);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Establishes the object that contains the files to
|
---|
86 | * be added to the output.
|
---|
87 | */
|
---|
88 | public Path createAddfiles() {
|
---|
89 | if (this.addfiles == null) {
|
---|
90 | this.addfiles = new Path(getProject());
|
---|
91 | }
|
---|
92 | return this.addfiles.createPath();
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Sets the files to be added into the output.
|
---|
97 | */
|
---|
98 | public void setAddfiles(Path addfiles) {
|
---|
99 | if (this.addfiles == null) {
|
---|
100 | this.addfiles = addfiles;
|
---|
101 | } else {
|
---|
102 | this.addfiles.append(addfiles);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Defines whether or not the output should be compacted.
|
---|
108 | */
|
---|
109 | public void setCompress(boolean compress) {
|
---|
110 | this.compress = compress;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Does the adding and merging.
|
---|
115 | */
|
---|
116 | public void execute() throws BuildException {
|
---|
117 | //Be sure everything has been set.
|
---|
118 | if (outfile == null) {
|
---|
119 | throw new BuildException("outfile attribute is required! "
|
---|
120 | + "Please set.");
|
---|
121 | }
|
---|
122 | if (!haveAddFiles() && !haveMergeFiles()) {
|
---|
123 | throw new BuildException("addfiles or mergefiles required! "
|
---|
124 | + "Please set.");
|
---|
125 | }
|
---|
126 | log("linking: " + outfile.getPath());
|
---|
127 | log("compression: " + compress, Project.MSG_VERBOSE);
|
---|
128 | jlink linker = new jlink();
|
---|
129 | linker.setOutfile(outfile.getPath());
|
---|
130 | linker.setCompression(compress);
|
---|
131 | if (haveMergeFiles()) {
|
---|
132 | log("merge files: " + mergefiles.toString(), Project.MSG_VERBOSE);
|
---|
133 | linker.addMergeFiles(mergefiles.list());
|
---|
134 | }
|
---|
135 | if (haveAddFiles()) {
|
---|
136 | log("add files: " + addfiles.toString(), Project.MSG_VERBOSE);
|
---|
137 | linker.addAddFiles(addfiles.list());
|
---|
138 | }
|
---|
139 | try {
|
---|
140 | linker.link();
|
---|
141 | } catch (Exception ex) {
|
---|
142 | throw new BuildException(ex, getLocation());
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | private boolean haveAddFiles() {
|
---|
147 | return haveEntries(addfiles);
|
---|
148 | }
|
---|
149 |
|
---|
150 | private boolean haveMergeFiles() {
|
---|
151 | return haveEntries(mergefiles);
|
---|
152 | }
|
---|
153 |
|
---|
154 | private boolean haveEntries(Path p) {
|
---|
155 | if (p == null) {
|
---|
156 | return false;
|
---|
157 | }
|
---|
158 | if (p.size() > 0) {
|
---|
159 | return true;
|
---|
160 | }
|
---|
161 | return false;
|
---|
162 | }
|
---|
163 |
|
---|
164 | private File outfile = null;
|
---|
165 |
|
---|
166 | private Path mergefiles = null;
|
---|
167 |
|
---|
168 | private Path addfiles = null;
|
---|
169 |
|
---|
170 | private boolean compress = false;
|
---|
171 |
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|