1 package org.apache.maven.model.path;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Objects;
26 import java.util.function.Function;
27
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.inject.Singleton;
31
32 import org.apache.maven.api.model.Build;
33 import org.apache.maven.api.model.Model;
34 import org.apache.maven.api.model.Reporting;
35 import org.apache.maven.api.model.Resource;
36 import org.apache.maven.model.building.ModelBuildingRequest;
37
38
39
40
41
42
43 @Named
44 @Singleton
45 public class DefaultModelPathTranslator
46 implements ModelPathTranslator
47 {
48
49 private final PathTranslator pathTranslator;
50
51 @Inject
52 public DefaultModelPathTranslator( PathTranslator pathTranslator )
53 {
54 this.pathTranslator = pathTranslator;
55 }
56
57 @Override
58 public void alignToBaseDirectory( org.apache.maven.model.Model modelV3, File basedir, ModelBuildingRequest request )
59 {
60 if ( modelV3 == null || basedir == null )
61 {
62 return;
63 }
64
65 Model model = modelV3.getDelegate();
66 Build build = model.getBuild();
67 Build newBuild = null;
68 if ( build != null )
69 {
70 newBuild = Build.newBuilder( build )
71 .directory( alignToBaseDirectory( build.getDirectory(), basedir ) )
72 .sourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) )
73 .testSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) )
74 .scriptSourceDirectory( alignToBaseDirectory( build.getScriptSourceDirectory(), basedir ) )
75 .resources( map( build.getResources(), r -> alignToBaseDirectory( r, basedir ) ) )
76 .testResources( map( build.getTestResources(), r -> alignToBaseDirectory( r, basedir ) ) )
77 .filters( map( build.getFilters(), s -> alignToBaseDirectory( s, basedir ) ) )
78 .outputDirectory( alignToBaseDirectory( build.getOutputDirectory(), basedir ) )
79 .testOutputDirectory( alignToBaseDirectory( build.getTestOutputDirectory(), basedir ) )
80 .build();
81 }
82
83 Reporting reporting = model.getReporting();
84 Reporting newReporting = null;
85 if ( reporting != null )
86 {
87 newReporting = Reporting.newBuilder( reporting )
88 .outputDirectory( alignToBaseDirectory( reporting.getOutputDirectory(), basedir ) )
89 .build();
90 }
91 if ( newBuild != build || newReporting != reporting )
92 {
93 modelV3.update( Model.newBuilder( model )
94 .build( newBuild )
95 .reporting( newReporting )
96 .build() );
97 }
98 }
99
100 private <T> List<T> map( List<T> resources, Function<T, T> mapper )
101 {
102 List<T> newResources = null;
103 if ( resources != null )
104 {
105 for ( int i = 0; i < resources.size(); i++ )
106 {
107 T resource = resources.get( i );
108 T newResource = mapper.apply( resource );
109 if ( newResource != null )
110 {
111 if ( newResources == null )
112 {
113 newResources = new ArrayList<>( resources );
114 }
115 newResources.set( i, newResource );
116 }
117 }
118 }
119 return newResources;
120 }
121
122 private Resource alignToBaseDirectory( Resource resource, File basedir )
123 {
124 if ( resource != null )
125 {
126 String newDir = alignToBaseDirectory( resource.getDirectory(), basedir );
127 if ( newDir != null )
128 {
129 return resource.withDirectory( newDir );
130 }
131 }
132 return resource;
133 }
134
135 private String alignToBaseDirectory( String path, File basedir )
136 {
137 String newPath = pathTranslator.alignToBaseDirectory( path, basedir );
138 return Objects.equals( path, newPath ) ? null : newPath;
139 }
140
141 }