Skip to content

Commit bf5845a

Browse files
committed
Warn instead of throwing on missing optional deps
1 parent 32dab63 commit bf5845a

File tree

5 files changed

+131
-17
lines changed

5 files changed

+131
-17
lines changed

log4j-converter-plugin-descriptor/pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
</dependency>
4848

4949
<!-- Compile dependencies: the artifact is shaded, so limit these. -->
50+
<dependency>
51+
<groupId>org.apache.logging.log4j</groupId>
52+
<artifactId>log4j-api</artifactId>
53+
</dependency>
54+
5055
<dependency>
5156
<groupId>info.picocli</groupId>
5257
<artifactId>picocli</artifactId>
@@ -66,4 +71,55 @@
6671

6772
</dependencies>
6873

74+
<build>
75+
<plugins>
76+
77+
<plugin>
78+
<groupId>org.apache.maven.plugins</groupId>
79+
<artifactId>maven-assembly-plugin</artifactId>
80+
<executions>
81+
<execution>
82+
<id>create-shaded-resources</id>
83+
<goals>
84+
<goal>single</goal>
85+
</goals>
86+
<phase>prepare-package</phase>
87+
<configuration>
88+
<inlineDescriptors>
89+
<assembly>
90+
<id>shaded-resources</id>
91+
<formats>
92+
<format>jar</format>
93+
</formats>
94+
<baseDirectory>/</baseDirectory>
95+
<fileSets>
96+
<fileSet>
97+
<directory>src/main/shaded-resources</directory>
98+
<outputDirectory>/</outputDirectory>
99+
</fileSet>
100+
</fileSets>
101+
</assembly>
102+
</inlineDescriptors>
103+
</configuration>
104+
</execution>
105+
</executions>
106+
</plugin>
107+
108+
<plugin>
109+
<groupId>org.apache.maven.plugins</groupId>
110+
<artifactId>maven-shade-plugin</artifactId>
111+
<executions>
112+
<execution>
113+
<id>shade-jar-with-dependencies</id>
114+
<configuration>
115+
<extraJars>
116+
<jar>${project.build.directory}/${project.build.finalName}-shaded-resources.jar</jar>
117+
</extraJars>
118+
</configuration>
119+
</execution>
120+
</executions>
121+
</plugin>
122+
123+
</plugins>
124+
</build>
69125
</project>

log4j-converter-plugin-descriptor/src/main/java/org/apache/logging/log4j/converter/plugins/internal/PluginDescriptors.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@
4545
import java.util.TreeMap;
4646
import java.util.TreeSet;
4747
import java.util.stream.Stream;
48+
import org.apache.logging.log4j.LogManager;
49+
import org.apache.logging.log4j.Logger;
4850

4951
public final class PluginDescriptors {
5052

5153
private static final String PLUGIN_BUILDER_FACTORY =
5254
"org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory";
5355
private static final String PLUGIN_FACTORY = "org.apache.logging.log4j.core.config.plugins.PluginFactory";
5456

57+
private static final Logger LOGGER = LogManager.getLogger(PluginDescriptors.class);
58+
5559
public static final class PluginDescriptor {
5660
private final Map<String, Namespace> namespacesByName;
5761

@@ -186,9 +190,20 @@ Namespace merge(final Namespace other) {
186190
}
187191

188192
Namespace withBuilderHierarchy(final ClassLoader classLoader) {
189-
final Map<String, Plugin> pluginsByClassName = new TreeMap<>(this.pluginsByClassName);
190-
pluginsByClassName.replaceAll((k, v) -> v.withBuilderHierarchy(classLoader));
191-
return new Namespace(name, Collections.unmodifiableMap(pluginsByClassName));
193+
final Map<String, Plugin> newPluginsByClassName = new TreeMap<>();
194+
pluginsByClassName.forEach((k, v) -> {
195+
try {
196+
newPluginsByClassName.put(k, v.withBuilderHierarchy(classLoader));
197+
} catch (final PluginNotFoundException e) {
198+
// No need to bother the user with the full cause
199+
// Since we use Simple Logger, we only print the NoClassDefFound message.
200+
LOGGER.warn(
201+
"Skipping plugin {} because it can not be loaded: {}",
202+
e.getMessage(),
203+
e.getCause().toString());
204+
}
205+
});
206+
return new Namespace(name, Collections.unmodifiableMap(newPluginsByClassName));
192207
}
193208

194209
private int countPluginNames(final Collection<Plugin> plugins) {
@@ -372,8 +387,7 @@ private Plugin merge(final Plugin other) {
372387
* @return A new plugin with the computed builder hierarchy.
373388
*/
374389
public Plugin withBuilderHierarchy(final ClassLoader classLoader) {
375-
final List<String> builderHierarchy =
376-
Collections.unmodifiableList(findBuilderClassHierarchy(classLoader, className));
390+
final List<String> builderHierarchy = Collections.unmodifiableList(findBuilderClassHierarchy(classLoader));
377391
return new Plugin(pluginNames, elementName, className, printable, defer, builderHierarchy);
378392
}
379393

@@ -388,7 +402,7 @@ public String toString() {
388402
+ builderHierarchy + '}';
389403
}
390404

391-
private static List<String> findBuilderClassHierarchy(final ClassLoader classLoader, final String className) {
405+
private List<String> findBuilderClassHierarchy(final ClassLoader classLoader) {
392406
try {
393407
final Class<?> pluginClass = classLoader.loadClass(className);
394408
for (final Method method : pluginClass.getMethods()) {
@@ -403,8 +417,8 @@ private static List<String> findBuilderClassHierarchy(final ClassLoader classLoa
403417
}
404418
}
405419
return Collections.emptyList();
406-
} catch (final ClassNotFoundException e) {
407-
throw new IllegalArgumentException("Unable to find class " + className, e);
420+
} catch (final ClassNotFoundException | LinkageError e) {
421+
throw new PluginNotFoundException(pluginNames, e);
408422
}
409423
}
410424

@@ -440,4 +454,11 @@ private static void readArray(final JsonParser parser, final Collection<? super
440454
assertArrayEnd(parser);
441455
}
442456
}
457+
458+
private static final class PluginNotFoundException extends RuntimeException {
459+
460+
private PluginNotFoundException(final Set<String> pluginNames, final Throwable cause) {
461+
super(pluginNames.toString(), cause);
462+
}
463+
}
443464
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to you under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. 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+
##
18+
# Enable Simple Logger
19+
#
20+
log4j.provider = org.apache.logging.log4j.simple.internal.SimpleProvider
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to you under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. 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+
##
18+
# Configures Simple Logger for the shaded package
19+
#
20+
log4j2.simplelogLevel = INFO

log4j-transform-parent/pom.xml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
<!-- plugin versions -->
4848
<jacoco-maven-plugin.version>0.8.12</jacoco-maven-plugin.version>
4949
<maven-invoker-plugin.version>3.6.1</maven-invoker-plugin.version>
50-
<maven-shade-plugin.version>3.5.0</maven-shade-plugin.version>
5150
<surefire.version>3.0.0-M7</surefire.version>
5251

5352
</properties>
@@ -158,12 +157,6 @@
158157
<version>${maven-invoker-plugin.version}</version>
159158
</plugin>
160159

161-
<plugin>
162-
<groupId>org.apache.maven</groupId>
163-
<artifactId>maven-shade-plugin</artifactId>
164-
<version>${maven-shade-plugin.version}</version>
165-
</plugin>
166-
167160
</plugins>
168161
</pluginManagement>
169162
</build>
@@ -220,7 +213,6 @@
220213
<plugin>
221214
<groupId>org.apache.maven.plugins</groupId>
222215
<artifactId>maven-shade-plugin</artifactId>
223-
<version>${maven-shade-plugin.version}</version>
224216
<executions>
225217
<execution>
226218
<id>shade-jar-with-dependencies</id>
@@ -234,6 +226,7 @@
234226
<excludes>
235227
<exclude>module-info.class</exclude>
236228
<exclude>META-INF/versions/*/module-info.class</exclude>
229+
<exclude>META-INF/DEPENDENCIES</exclude>
237230
<exclude>META-INF/MANIFEST.MF</exclude>
238231
</excludes>
239232
</filter>
@@ -244,7 +237,11 @@
244237
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
245238
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer" />
246239
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
247-
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" />
240+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
241+
<manifestEntries>
242+
<Multi-Release>true</Multi-Release>
243+
</manifestEntries>
244+
</transformer>
248245
</transformers>
249246
</configuration>
250247
</execution>

0 commit comments

Comments
 (0)