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.cling.invoker.mvnup.goals;
20  
21  import java.io.StringReader;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.jdom2.Document;
26  import org.jdom2.input.SAXBuilder;
27  
28  /**
29   * Builder for creating test POM documents with fluent API.
30   */
31  public class PomBuilder {
32  
33      private String modelVersion = "4.0.0";
34      private String namespace = "http://maven.apache.org/POM/4.0.0";
35      private String groupId;
36      private String artifactId;
37      private String version;
38      private String packaging;
39      private Parent parent;
40      private final List<Dependency> dependencies = new ArrayList<>();
41      private final List<Plugin> plugins = new ArrayList<>();
42      private final List<Property> properties = new ArrayList<>();
43  
44      public static PomBuilder create() {
45          return new PomBuilder();
46      }
47  
48      public PomBuilder modelVersion(String modelVersion) {
49          this.modelVersion = modelVersion;
50          return this;
51      }
52  
53      public PomBuilder namespace(String namespace) {
54          this.namespace = namespace;
55          return this;
56      }
57  
58      public PomBuilder groupId(String groupId) {
59          this.groupId = groupId;
60          return this;
61      }
62  
63      public PomBuilder artifactId(String artifactId) {
64          this.artifactId = artifactId;
65          return this;
66      }
67  
68      public PomBuilder version(String version) {
69          this.version = version;
70          return this;
71      }
72  
73      public PomBuilder packaging(String packaging) {
74          this.packaging = packaging;
75          return this;
76      }
77  
78      public PomBuilder parent(String groupId, String artifactId, String version) {
79          this.parent = new Parent(groupId, artifactId, version);
80          return this;
81      }
82  
83      public PomBuilder dependency(String groupId, String artifactId, String version) {
84          this.dependencies.add(new Dependency(groupId, artifactId, version, null));
85          return this;
86      }
87  
88      public PomBuilder dependency(String groupId, String artifactId, String version, String scope) {
89          this.dependencies.add(new Dependency(groupId, artifactId, version, scope));
90          return this;
91      }
92  
93      public PomBuilder plugin(String groupId, String artifactId, String version) {
94          this.plugins.add(new Plugin(groupId, artifactId, version));
95          return this;
96      }
97  
98      public PomBuilder property(String name, String value) {
99          this.properties.add(new Property(name, value));
100         return this;
101     }
102 
103     public String build() {
104         StringBuilder xml = new StringBuilder();
105         xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
106         xml.append("<project xmlns=\"").append(namespace).append("\">\n");
107         if (modelVersion != null) {
108             xml.append("    <modelVersion>").append(modelVersion).append("</modelVersion>\n");
109         }
110 
111         if (parent != null) {
112             xml.append("    <parent>\n");
113             xml.append("        <groupId>").append(parent.groupId).append("</groupId>\n");
114             xml.append("        <artifactId>").append(parent.artifactId).append("</artifactId>\n");
115             xml.append("        <version>").append(parent.version).append("</version>\n");
116             xml.append("    </parent>\n");
117         }
118 
119         if (groupId != null) {
120             xml.append("    <groupId>").append(groupId).append("</groupId>\n");
121         }
122         if (artifactId != null) {
123             xml.append("    <artifactId>").append(artifactId).append("</artifactId>\n");
124         }
125         if (version != null) {
126             xml.append("    <version>").append(version).append("</version>\n");
127         }
128         if (packaging != null) {
129             xml.append("    <packaging>").append(packaging).append("</packaging>\n");
130         }
131 
132         if (!properties.isEmpty()) {
133             xml.append("    <properties>\n");
134             for (Property property : properties) {
135                 xml.append("        <")
136                         .append(property.name)
137                         .append(">")
138                         .append(property.value)
139                         .append("</")
140                         .append(property.name)
141                         .append(">\n");
142             }
143             xml.append("    </properties>\n");
144         }
145 
146         if (!dependencies.isEmpty()) {
147             xml.append("    <dependencies>\n");
148             for (Dependency dependency : dependencies) {
149                 xml.append("        <dependency>\n");
150                 xml.append("            <groupId>").append(dependency.groupId).append("</groupId>\n");
151                 xml.append("            <artifactId>")
152                         .append(dependency.artifactId)
153                         .append("</artifactId>\n");
154                 xml.append("            <version>").append(dependency.version).append("</version>\n");
155                 if (dependency.scope != null) {
156                     xml.append("            <scope>").append(dependency.scope).append("</scope>\n");
157                 }
158                 xml.append("        </dependency>\n");
159             }
160             xml.append("    </dependencies>\n");
161         }
162 
163         if (!plugins.isEmpty()) {
164             xml.append("    <build>\n");
165             xml.append("        <plugins>\n");
166             for (Plugin plugin : plugins) {
167                 xml.append("            <plugin>\n");
168                 xml.append("                <groupId>").append(plugin.groupId).append("</groupId>\n");
169                 xml.append("                <artifactId>")
170                         .append(plugin.artifactId)
171                         .append("</artifactId>\n");
172                 xml.append("                <version>").append(plugin.version).append("</version>\n");
173                 xml.append("            </plugin>\n");
174             }
175             xml.append("        </plugins>\n");
176             xml.append("    </build>\n");
177         }
178 
179         xml.append("</project>\n");
180         return xml.toString();
181     }
182 
183     public Document buildDocument() {
184         try {
185             SAXBuilder saxBuilder = new SAXBuilder();
186             return saxBuilder.build(new StringReader(build()));
187         } catch (Exception e) {
188             throw new RuntimeException("Failed to build POM document", e);
189         }
190     }
191 
192     private record Parent(String groupId, String artifactId, String version) {}
193 
194     private record Dependency(String groupId, String artifactId, String version, String scope) {}
195 
196     private record Plugin(String groupId, String artifactId, String version) {}
197 
198     private record Property(String name, String value) {}
199 }