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