15
15
import org .apache .maven .project .ProjectBuildingRequest ;
16
16
import org .apache .maven .shared .dependency .graph .DependencyCollectorBuilder ;
17
17
import org .apache .maven .shared .dependency .graph .DependencyNode ;
18
+ import org .eclipse .sisu .Nullable ;
18
19
19
20
import java .io .File ;
20
21
import java .io .IOException ;
21
22
import java .nio .file .Files ;
22
- import java .nio .file .Path ;
23
23
import java .util .ArrayList ;
24
24
import java .util .HashSet ;
25
25
import java .util .List ;
@@ -74,20 +74,22 @@ public void execute() throws MojoExecutionException {
74
74
Set <DependencyNode > hostAppDependencies = new HashSet <>();
75
75
DependencyNode hostDep = null ;
76
76
SimpleArtifact hostAppArtifact = getHostAppArtifact ();
77
+
77
78
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 );
88
84
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
+ }
91
93
92
94
93
95
// Build assembly descriptor
@@ -135,17 +137,18 @@ public void execute() throws MojoExecutionException {
135
137
private SimpleArtifact getHostAppArtifact () {
136
138
SimpleArtifact hostAppArtifact = null ;
137
139
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 ;
139
142
}
140
143
if (naeVersion != null && !naeVersion .isEmpty ()) {
141
144
hostAppArtifact = new SimpleArtifact (Constants .NETGRIF_GROUP_ID , Constants .NAE_ARTIFACT_ID , naeVersion );
142
- } else if (hostApp != null && hostApp .isValid ()) {
145
+ } else if (hostApp .isValid ()) {
143
146
hostAppArtifact = hostApp ;
144
147
}
145
148
return hostAppArtifact ;
146
149
}
147
150
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 ) {
149
152
Artifact depArtifact = node .getArtifact ();
150
153
if (depArtifact .getGroupId ().equalsIgnoreCase (groupId ) && depArtifact .getArtifactId ().equalsIgnoreCase (artifactId ) && depArtifact .getVersion ().equalsIgnoreCase (version )) {
151
154
return node ;
@@ -161,9 +164,9 @@ private DependencyNode findDependency(String groupId, String artifactId, String
161
164
return null ;
162
165
}
163
166
164
- private Set <DependencyNode > flattenDependencies (DependencyNode parent ) {
167
+ private static Set <DependencyNode > flattenDependencies (DependencyNode parent ) {
165
168
Set <DependencyNode > dependencies = new HashSet <>();
166
- if (!parent .getChildren ().isEmpty ()) {
169
+ if (parent != null && !parent .getChildren ().isEmpty ()) {
167
170
parent .getChildren ().forEach (dep -> {
168
171
dependencies .add (dep );
169
172
dependencies .addAll (flattenDependencies (dep ));
@@ -172,7 +175,7 @@ private Set<DependencyNode> flattenDependencies(DependencyNode parent) {
172
175
return dependencies ;
173
176
}
174
177
175
- public void separateDescriptor (AssemblyDescriptorBuilder assembly , DependencyNode hostDep , Set <DependencyNode > hostAppDependencies ) {
178
+ public void separateDescriptor (AssemblyDescriptorBuilder assembly , @ Nullable DependencyNode hostDep , @ Nullable Set <DependencyNode > hostAppDependencies ) {
176
179
assembly .format ("zip" );
177
180
assembly .includeBaseDirectory (false );
178
181
DependencySetBuilder depSet = assembly .dependencySets ()
@@ -182,15 +185,27 @@ public void separateDescriptor(AssemblyDescriptorBuilder assembly, DependencyNod
182
185
.useTransitiveFiltering (true )
183
186
.unpack (false )
184
187
.scope ("runtime" );
185
- if (hostDep != null )
188
+ if (hostDep != null ) {
186
189
depSet .exclude (hostDep );
187
- hostAppDependencies .forEach (depSet ::exclude );
190
+ }
191
+ if (hostAppDependencies != null ) {
192
+ hostAppDependencies .forEach (depSet ::exclude );
193
+ }
188
194
if (log .isDebugEnabled ())
189
195
log .debug ("Excluding extra dependencies: " + excludes );
190
196
excludes .forEach (depSet ::exclude );
191
197
}
192
198
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 ) {
194
209
assembly .format ("zip" );
195
210
assembly .includeBaseDirectory (false );
196
211
assembly .fileSets ().fileSet ()
@@ -204,9 +219,12 @@ public void singleDescriptor(AssemblyDescriptorBuilder assembly, DependencyNode
204
219
.useTransitiveFiltering (true )
205
220
.unpack (false )
206
221
.scope ("runtime" );
207
- if (hostDep != null )
222
+ if (hostDep != null ) {
208
223
depSet .exclude (hostDep );
209
- hostAppDependencies .forEach (depSet ::exclude );
224
+ }
225
+ if (hostAppDependencies != null ) {
226
+ hostAppDependencies .forEach (depSet ::exclude );
227
+ }
210
228
if (log .isDebugEnabled ())
211
229
log .debug ("Excluding extra dependencies: " + excludes );
212
230
excludes .forEach (depSet ::exclude );
0 commit comments