View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.enforcer.rules.utils;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.maven.RepositoryUtils;
25  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
26  import org.eclipse.aether.artifact.Artifact;
27  import org.eclipse.aether.artifact.ArtifactType;
28  import org.eclipse.aether.artifact.DefaultArtifact;
29  import org.eclipse.aether.graph.DefaultDependencyNode;
30  import org.eclipse.aether.graph.Dependency;
31  import org.eclipse.aether.graph.DependencyNode;
32  
33  import static java.util.Optional.ofNullable;
34  import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE;
35  
36  public class DependencyNodeBuilder {
37      public enum Type {
38          WAR(RepositoryUtils.newArtifactType("war", new DefaultArtifactHandler("war"))),
39          JAR(RepositoryUtils.newArtifactType("jar", new DefaultArtifactHandler("jar"))),
40          POM(RepositoryUtils.newArtifactType("pom", new DefaultArtifactHandler("pom")));
41  
42          private final ArtifactType artifactType;
43  
44          Type(ArtifactType artifactType) {
45              this.artifactType = artifactType;
46          }
47  
48          ArtifactType asArtifactType() {
49              return artifactType;
50          }
51  
52          String asString() {
53              return name().toLowerCase();
54          }
55      }
56  
57      private String groupId;
58  
59      private String artifactId;
60  
61      private String version;
62  
63      private Type type;
64  
65      private String scope;
66  
67      private boolean optional;
68  
69      private List<DependencyNode> children = new ArrayList<>();
70  
71      public DependencyNodeBuilder withGroupId(String val) {
72          groupId = val;
73          return this;
74      }
75  
76      public DependencyNodeBuilder withArtifactId(String val) {
77          artifactId = val;
78          return this;
79      }
80  
81      public DependencyNodeBuilder withVersion(String val) {
82          version = val;
83          return this;
84      }
85  
86      public DependencyNodeBuilder withType(Type val) {
87          type = val;
88          return this;
89      }
90  
91      public DependencyNodeBuilder withScope(String val) {
92          scope = val;
93          return this;
94      }
95  
96      public DependencyNodeBuilder withOptional(boolean val) {
97          optional = val;
98          return this;
99      }
100 
101     public DependencyNodeBuilder withChildNode(DependencyNode val) {
102         children.add(val);
103         return this;
104     }
105 
106     public DependencyNode build() {
107         Artifact artifact = new DefaultArtifact(
108                 ofNullable(groupId).orElse("default-group"),
109                 ofNullable(artifactId).orElse("default-artifact"),
110                 "classifier",
111                 ofNullable(type).map(Type::asString).orElse("pom"),
112                 ofNullable(version).orElse("default-version"),
113                 ofNullable(type).orElse(Type.JAR).asArtifactType());
114         Dependency dependency = new Dependency(artifact, ofNullable(scope).orElse(SCOPE_COMPILE), optional);
115         DependencyNode instance = new DefaultDependencyNode(dependency);
116         instance.setChildren(children);
117         return instance;
118     }
119 }