1 /**
2 *
3 * Copyright 2018 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.codehaus.plexus.archiver.jar;
18
19 /**
20 * Base class for creating modular JAR archives.
21 *
22 * Subclasses are required to be able to handle both
23 * JAR archives with module descriptor (modular JAR)
24 * and without ("regular" JAR).
25 * That would allow clients of this class to use
26 * it without prior knowledge if the classes
27 * they are going to add are part of module
28 * (contain module descriptor class) or not.
29 *
30 * <p>The class allows you to set the
31 * module main class ({@link #setModuleMainClass(String)}),
32 * but if it is not set or it is set to {@code null},
33 * then the {@code Main-Class} attribute of the
34 * JAR manifest is used (if present) to set
35 * the module main class.
36 *
37 * @since 3.6
38 */
39 public abstract class ModularJarArchiver extends JarArchiver {
40 private String moduleMainClass;
41
42 private String manifestMainClass;
43
44 private String moduleVersion;
45
46 public String getModuleMainClass() {
47 return moduleMainClass;
48 }
49
50 /**
51 * Sets the module main class.
52 * Ignored if the JAR file does not contain
53 * module descriptor.
54 *
55 * <p>Note that implementations may choose
56 * to replace the value set in the manifest as well.
57 *
58 * @param moduleMainClass the module main class.
59 */
60 public void setModuleMainClass(String moduleMainClass) {
61 this.moduleMainClass = moduleMainClass;
62 }
63
64 public String getModuleVersion() {
65 return moduleVersion;
66 }
67
68 /**
69 * Sets the module version.
70 * Ignored if the JAR file does not contain
71 * module descriptor.
72 *
73 * @param moduleVersion the module version.
74 */
75 public void setModuleVersion(String moduleVersion) {
76 this.moduleVersion = moduleVersion;
77 }
78
79 /**
80 * Returns the "Main-Class" attribute of the
81 * manifest added to the archive.
82 *
83 * {@code null} if there is no manifest
84 * or the attribute is not set.
85 *
86 * @return the "Main-Class" attribute of the manifest
87 */
88 protected String getManifestMainClass() {
89 return manifestMainClass;
90 }
91
92 @Override
93 protected Manifest createManifest() {
94 Manifest manifest = super.createManifest();
95
96 if (manifest != null) {
97 manifestMainClass = manifest.getMainAttributes().getValue("Main-Class");
98 }
99
100 return manifest;
101 }
102
103 @Override
104 public void reset() {
105 // We want to be sure that on multiple run
106 // the latest manifest is used, so lets
107 // reset it to null
108 manifestMainClass = null;
109 super.reset();
110 }
111 }