Skip to content

Commit 81901ef

Browse files
committed
Handle null values and missing host application gracefully
- Refactored methods to handle null values more robustly, including nullable annotations and additional checks. - Improved logging to warn when packaging the module without a host application instead of failing. - Updated plugin version to 1.0.1.
1 parent 383ee83 commit 81901ef

File tree

3 files changed

+48
-26
lines changed

3 files changed

+48
-26
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.netgrif</groupId>
88
<artifactId>nae-module-maven-plugin</artifactId>
9-
<version>1.0.0</version>
9+
<version>1.0.1</version>
1010
<packaging>maven-plugin</packaging>
1111

1212
<name>nae-module-maven-plugin</name>

src/main/java/com/netgrif/maven/plugin/module/BuildModuleMojo.java

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
import org.apache.maven.project.ProjectBuildingRequest;
1616
import org.apache.maven.shared.dependency.graph.DependencyCollectorBuilder;
1717
import org.apache.maven.shared.dependency.graph.DependencyNode;
18+
import org.eclipse.sisu.Nullable;
1819

1920
import java.io.File;
2021
import java.io.IOException;
2122
import java.nio.file.Files;
22-
import java.nio.file.Path;
2323
import java.util.ArrayList;
2424
import java.util.HashSet;
2525
import java.util.List;
@@ -74,20 +74,22 @@ public void execute() throws MojoExecutionException {
7474
Set<DependencyNode> hostAppDependencies = new HashSet<>();
7575
DependencyNode hostDep = null;
7676
SimpleArtifact hostAppArtifact = getHostAppArtifact();
77+
7778
if (hostAppArtifact == null) {
78-
throw new HostApplicationNotFoundException("Host application cannot be resolved");
79-
}
80-
hostDep = findDependency(hostAppArtifact.getGroupId(), hostAppArtifact.getArtifactId(), hostAppArtifact.getVersion(), rootNode);
81-
if (hostDep == null || hostDep.getArtifact() == null)
82-
throw new HostApplicationNotFoundException("Cannot find host app artifact as dependency: " + hostAppArtifact);
83-
if (log.isDebugEnabled())
84-
log.debug("Host app dependency: " + hostDep.getArtifact());
85-
hostAppDependencies = flattenDependencies(hostDep);
86-
log.info("Dependencies aggregated " + hostAppDependencies.size() + " from host app");
87-
hostAppDependencies.forEach(d -> {
79+
log.warn("Packaging module without host application");
80+
} else {
81+
hostDep = findDependency(hostAppArtifact.getGroupId(), hostAppArtifact.getArtifactId(), hostAppArtifact.getVersion(), rootNode);
82+
if (hostDep == null || hostDep.getArtifact() == null)
83+
throw new HostApplicationNotFoundException("Cannot find host app artifact as dependency: " + hostAppArtifact);
8884
if (log.isDebugEnabled())
89-
log.debug(d.getArtifact().toString());
90-
});
85+
log.debug("Host app dependency: " + hostDep.getArtifact());
86+
hostAppDependencies = flattenDependencies(hostDep);
87+
log.info("Dependencies aggregated " + hostAppDependencies.size() + " from host app");
88+
hostAppDependencies.forEach(d -> {
89+
if (log.isDebugEnabled())
90+
log.debug(d.getArtifact().toString());
91+
});
92+
}
9193

9294

9395
// Build assembly descriptor
@@ -135,17 +137,18 @@ public void execute() throws MojoExecutionException {
135137
private SimpleArtifact getHostAppArtifact() {
136138
SimpleArtifact hostAppArtifact = null;
137139
if (hostApp == null && (naeVersion == null || naeVersion.isEmpty())) {
138-
throw new HostApplicationNotFoundException("Host application or NAE version not found. At least one must be defined");
140+
log.warn("Host application was not found. Packaging module without host application");
141+
return null;
139142
}
140143
if (naeVersion != null && !naeVersion.isEmpty()) {
141144
hostAppArtifact = new SimpleArtifact(Constants.NETGRIF_GROUP_ID, Constants.NAE_ARTIFACT_ID, naeVersion);
142-
} else if (hostApp != null && hostApp.isValid()) {
145+
} else if (hostApp.isValid()) {
143146
hostAppArtifact = hostApp;
144147
}
145148
return hostAppArtifact;
146149
}
147150

148-
private DependencyNode findDependency(String groupId, String artifactId, String version, DependencyNode node) {
151+
private static DependencyNode findDependency(String groupId, String artifactId, String version, DependencyNode node) {
149152
Artifact depArtifact = node.getArtifact();
150153
if (depArtifact.getGroupId().equalsIgnoreCase(groupId) && depArtifact.getArtifactId().equalsIgnoreCase(artifactId) && depArtifact.getVersion().equalsIgnoreCase(version)) {
151154
return node;
@@ -161,9 +164,9 @@ private DependencyNode findDependency(String groupId, String artifactId, String
161164
return null;
162165
}
163166

164-
private Set<DependencyNode> flattenDependencies(DependencyNode parent) {
167+
private static Set<DependencyNode> flattenDependencies(DependencyNode parent) {
165168
Set<DependencyNode> dependencies = new HashSet<>();
166-
if (!parent.getChildren().isEmpty()) {
169+
if (parent != null && !parent.getChildren().isEmpty()) {
167170
parent.getChildren().forEach(dep -> {
168171
dependencies.add(dep);
169172
dependencies.addAll(flattenDependencies(dep));
@@ -172,7 +175,7 @@ private Set<DependencyNode> flattenDependencies(DependencyNode parent) {
172175
return dependencies;
173176
}
174177

175-
public void separateDescriptor(AssemblyDescriptorBuilder assembly, DependencyNode hostDep, Set<DependencyNode> hostAppDependencies) {
178+
public void separateDescriptor(AssemblyDescriptorBuilder assembly, @Nullable DependencyNode hostDep, @Nullable Set<DependencyNode> hostAppDependencies) {
176179
assembly.format("zip");
177180
assembly.includeBaseDirectory(false);
178181
DependencySetBuilder depSet = assembly.dependencySets()
@@ -182,15 +185,27 @@ public void separateDescriptor(AssemblyDescriptorBuilder assembly, DependencyNod
182185
.useTransitiveFiltering(true)
183186
.unpack(false)
184187
.scope("runtime");
185-
if (hostDep != null)
188+
if (hostDep != null) {
186189
depSet.exclude(hostDep);
187-
hostAppDependencies.forEach(depSet::exclude);
190+
}
191+
if (hostAppDependencies != null) {
192+
hostAppDependencies.forEach(depSet::exclude);
193+
}
188194
if (log.isDebugEnabled())
189195
log.debug("Excluding extra dependencies: " + excludes);
190196
excludes.forEach(depSet::exclude);
191197
}
192198

193-
public void singleDescriptor(AssemblyDescriptorBuilder assembly, DependencyNode hostDep, Set<DependencyNode> hostAppDependencies) {
199+
/**
200+
* Configures an assembly descriptor to generate a single descriptor containing specific files
201+
* and dependencies, while excluding certain unnecessary ones. This method customizes the assembly
202+
* builder by defining file sets and dependency sets, including exclusions for dependencies specified.
203+
*
204+
* @param assembly the {@link AssemblyDescriptorBuilder} used to configure the assembly descriptor
205+
* @param hostDep the {@link DependencyNode} representing the main host dependency to exclude, can be null
206+
* @param hostAppDependencies the {@link Set} of {@link DependencyNode} representing the additional host application dependencies to exclude, can be null
207+
*/
208+
public void singleDescriptor(AssemblyDescriptorBuilder assembly, @Nullable DependencyNode hostDep, @Nullable Set<DependencyNode> hostAppDependencies) {
194209
assembly.format("zip");
195210
assembly.includeBaseDirectory(false);
196211
assembly.fileSets().fileSet()
@@ -204,9 +219,12 @@ public void singleDescriptor(AssemblyDescriptorBuilder assembly, DependencyNode
204219
.useTransitiveFiltering(true)
205220
.unpack(false)
206221
.scope("runtime");
207-
if (hostDep != null)
222+
if (hostDep != null) {
208223
depSet.exclude(hostDep);
209-
hostAppDependencies.forEach(depSet::exclude);
224+
}
225+
if (hostAppDependencies != null) {
226+
hostAppDependencies.forEach(depSet::exclude);
227+
}
210228
if (log.isDebugEnabled())
211229
log.debug("Excluding extra dependencies: " + excludes);
212230
excludes.forEach(depSet::exclude);

src/main/java/com/netgrif/maven/plugin/module/parameters/SimpleArtifact.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class SimpleArtifact {
1717
private String version;
1818

1919
public SimpleArtifact(String artifact) {
20+
if (artifact == null || artifact.isBlank()) return;
2021
String[] split = artifact.split(SEPARATOR);
2122
this.groupId = split[0];
2223
if (split.length > 1) {
@@ -28,11 +29,14 @@ public SimpleArtifact(String artifact) {
2829
}
2930

3031
public boolean equalsToArtifact(Artifact artifact) {
32+
if (artifact == null) return false;
3133
return this.groupId.equals(artifact.getGroupId()) && this.artifactId.equals(artifact.getArtifactId()) && this.version.equals(artifact.getVersion());
3234
}
3335

3436
public boolean isValid() {
35-
return !groupId.isBlank() && !artifactId.isBlank() && !version.isBlank();
37+
return groupId != null && !groupId.isBlank() &&
38+
artifactId != null && !artifactId.isBlank() &&
39+
version != null && !version.isBlank();
3640
}
3741

3842
@Override

0 commit comments

Comments
 (0)