View Javadoc
1   package org.apache.maven.model.building;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  import java.nio.file.Path;
24  import java.util.Optional;
25  import java.util.function.BiFunction;
26  import java.util.function.Function;
27  
28  import org.apache.maven.model.Model;
29  import org.apache.maven.model.transform.BuildToRawPomXMLFilterFactory;
30  import org.apache.maven.model.transform.RelativeProject;
31  
32  /**
33   * A BuildPomXMLFilterFactory which is context aware
34   *
35   * @author Robert Scholte
36   * @since 4.0.0
37   */
38  public class DefaultBuildPomXMLFilterFactory extends BuildToRawPomXMLFilterFactory
39  {
40      private final TransformerContext context;
41  
42      /**
43       *
44       * @param context a set of data to extract values from as required for the build pom
45       * @param consume {@code true} if this factory is being used for creating the consumer pom, otherwise {@code false}
46       */
47      public DefaultBuildPomXMLFilterFactory( TransformerContext context,
48                                              boolean consume )
49      {
50          super( consume );
51          this.context = context;
52      }
53  
54      @Override
55      protected Function<Path, Optional<RelativeProject>> getRelativePathMapper()
56      {
57          return p -> Optional.ofNullable( context.getRawModel( p ) )
58                  .map( DefaultBuildPomXMLFilterFactory::toRelativeProject );
59      }
60  
61      @Override
62      protected BiFunction<String, String, String> getDependencyKeyToVersionMapper()
63      {
64          return ( g, a ) -> Optional.ofNullable( context.getRawModel( g, a ) )
65                              .map( DefaultBuildPomXMLFilterFactory::toVersion )
66                              .orElse( null );
67      }
68  
69      @Override
70      protected Optional<String> getChangelist()
71      {
72          return Optional.ofNullable( context.getUserProperty( "changelist" ) );
73      }
74  
75      @Override
76      protected Optional<String> getRevision()
77      {
78          return Optional.ofNullable( context.getUserProperty( "revision" ) );
79      }
80  
81      @Override
82      protected Optional<String> getSha1()
83      {
84          return Optional.ofNullable( context.getUserProperty( "sha1" ) );
85      }
86  
87      private static RelativeProject toRelativeProject( final Model m )
88      {
89          String groupId = m.getGroupId();
90          if ( groupId == null && m.getParent() != null )
91          {
92              groupId = m.getParent().getGroupId();
93          }
94  
95          String version = m.getVersion();
96          if ( version == null && m.getParent() != null )
97          {
98              version = m.getParent().getVersion();
99          }
100 
101         return new RelativeProject( groupId, m.getArtifactId(), version );
102     }
103 
104     private static String toVersion( final Model m )
105     {
106         String version = m.getVersion();
107         if ( version == null && m.getParent() != null )
108         {
109             version = m.getParent().getVersion();
110         }
111 
112         return version;
113     }
114 }