Skip to content

Commit fb75779

Browse files
authored
Merge pull request #615 from bci-oss/606-handle-multiple-aspects-in-maven-plugin
Correctly handle models containing multiple Aspects in Maven plugin
2 parents 7dab5f5 + 9ddda6e commit fb75779

File tree

3 files changed

+77
-25
lines changed

3 files changed

+77
-25
lines changed

tools/esmf-aspect-model-maven-plugin/src/main/java/org/eclipse/esmf/aspectmodel/AspectModelMojo.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
package org.eclipse.esmf.aspectmodel;
1515

16-
import static java.util.stream.Collectors.toSet;
17-
1816
import java.io.FileOutputStream;
1917
import java.io.IOException;
2018
import java.nio.file.Files;
2119
import java.nio.file.Path;
20+
import java.util.HashMap;
2221
import java.util.HashSet;
2322
import java.util.List;
23+
import java.util.Map;
2424
import java.util.Set;
2525

2626
import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
@@ -59,35 +59,37 @@ protected void validateParameters() throws MojoExecutionException {
5959
}
6060

6161
protected Set<Aspect> loadAspects() throws MojoExecutionException {
62-
Set<AspectModel> models = loadModels();
63-
return models.stream().map( AspectModel::aspect ).collect( toSet() );
62+
return new HashSet<>( loadAspectModels().values() );
6463
}
6564

6665
protected Set<AspectModel> loadModels() throws MojoExecutionException {
67-
try {
68-
final Path modelsRoot = Path.of( modelsRootDirectory );
69-
final ResolutionStrategy fileSystemStrategy = new FileSystemStrategy( modelsRoot );
70-
final Set<AspectModel> result = new HashSet<>();
66+
return new HashSet<>( loadAspectModels().keySet() );
67+
}
68+
69+
private Map<AspectModel, Aspect> loadAspectModels() throws MojoExecutionException {
70+
final Path modelsRoot = Path.of( modelsRootDirectory );
71+
final ResolutionStrategy fileSystemStrategy = new FileSystemStrategy( modelsRoot );
72+
final Map<AspectModel, Aspect> result = new HashMap<>();
7173

72-
for ( final String inputUrn : includes ) {
73-
final AspectModelUrn urn = AspectModelUrn.fromUrn( inputUrn );
74-
final Either<List<Violation>, AspectModel> loadingResult = new AspectModelValidator().loadModel( () ->
75-
new AspectModelLoader( fileSystemStrategy ).load( urn ) );
76-
if ( loadingResult.isLeft() ) {
77-
final List<Violation> violations = loadingResult.getLeft();
78-
final String errorMessage = detailedValidationMessages
79-
? new DetailedViolationFormatter().apply( violations )
80-
: new ViolationFormatter().apply( violations );
81-
throw new MojoExecutionException( errorMessage );
82-
}
83-
result.add( loadingResult.get() );
74+
for ( final String inputUrn : includes ) {
75+
final AspectModelUrn urn = AspectModelUrn.fromUrn( inputUrn );
76+
final Either<List<Violation>, AspectModel> loadingResult = new AspectModelValidator().loadModel( () ->
77+
new AspectModelLoader( fileSystemStrategy ).load( urn ) );
78+
if ( loadingResult.isLeft() ) {
79+
final List<Violation> violations = loadingResult.getLeft();
80+
final String errorMessage = detailedValidationMessages
81+
? new DetailedViolationFormatter().apply( violations )
82+
: new ViolationFormatter().apply( violations );
83+
throw new MojoExecutionException( errorMessage );
8484
}
85-
return result;
86-
} catch ( final MojoExecutionException exception ) {
87-
throw exception;
88-
} catch ( final Throwable throwable ) {
89-
throw new MojoExecutionException( "Processing error while loading Aspects", throwable );
85+
final AspectModel aspectModel = loadingResult.get();
86+
final Aspect aspect = aspectModel.aspects().stream()
87+
.filter( theAspect -> theAspect.urn().equals( urn ) )
88+
.findFirst()
89+
.orElseThrow( () -> new MojoExecutionException( "Loaded Aspect Model does not contain Aspect " + urn ) );
90+
result.put( aspectModel, aspect );
9091
}
92+
return result;
9193
}
9294

9395
protected FileOutputStream getOutputStreamForFile( final String artifactName, final String outputDirectory ) {

tools/esmf-aspect-model-maven-plugin/src/test/java/org/eclipse/esmf/aspectmodel/ValidateTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,12 @@ public void testValidateInvalidAspectModel() throws Exception {
3838
.isInstanceOf( MojoExecutionException.class )
3939
.hasMessageContaining( "Syntax error in line 17, column 2" );
4040
}
41+
42+
@Test
43+
public void testValidateMultipleAspectModels() throws Exception {
44+
final File testPom = getTestFile( "src/test/resources/validate-pom-multiple-aspect-models.xml" );
45+
final Mojo validate = lookupMojo( "validate", testPom );
46+
assertThatCode( validate::execute )
47+
.doesNotThrowAnyException();
48+
}
4149
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2022 Robert Bosch Manufacturing Solutions GmbH
4+
~
5+
~ See the AUTHORS file(s) distributed with this work for additional
6+
~ information regarding authorship.
7+
~
8+
~ This Source Code Form is subject to the terms of the Mozilla Public
9+
~ License, v. 2.0. If a copy of the MPL was not distributed with this
10+
~ file, You can obtain one at https://mozilla.org/MPL/2.0/.
11+
~
12+
~ SPDX-License-Identifier: MPL-2.0
13+
-->
14+
15+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
17+
<modelVersion>4.0.0</modelVersion>
18+
19+
<groupId>org.eclipse.esmf</groupId>
20+
<artifactId>test-validate-mojo</artifactId>
21+
<version>1.0</version>
22+
<packaging>jar</packaging>
23+
<name>Test Validate Mojo</name>
24+
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<artifactId>esmf-aspect-model-maven-plugin</artifactId>
29+
<configuration>
30+
<modelsRootDirectory>${basedir}/../../core/esmf-test-aspect-models/src/main/resources/valid</modelsRootDirectory>
31+
<includes>
32+
<include>urn:samm:org.eclipse.esmf.test:1.0.0#Aspect</include>
33+
<include>urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithBlankNode</include>
34+
<include>urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithComplexSet</include>
35+
<include>urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithList</include>
36+
<include>urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithSee</include>
37+
</includes>
38+
</configuration>
39+
</plugin>
40+
</plugins>
41+
</build>
42+
</project>

0 commit comments

Comments
 (0)