-
Notifications
You must be signed in to change notification settings - Fork 0
/
Setup.java
139 lines (117 loc) · 5.27 KB
/
Setup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.io.File;
import java.io.FileOutputStream;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Setup {
public static void main(String[] args) throws Exception {
File file = new File(System.getProperty("user.home") + "/.m2/settings.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);
Node settings = document.getElementsByTagName("settings").item(0);
servers(document, settings);
profiles(document, settings);
activeProfiles(document, settings);
StringWriter output = new StringWriter();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult(output));
FileOutputStream fos = new FileOutputStream(file, false);
fos.write(output.toString().getBytes("UTF-8"));
fos.close();
}
public static void servers(Document document, Node settings) {
Node servers = hasChild(settings, "servers");
if (servers == null) {
servers = settings.appendChild(document.createElement("servers"));
}
Node server = find(servers, "id", "ossrh");
if (server == null) {
server = servers.appendChild(document.createElement("server"));
}
removeChildren(server);
Node id = server.appendChild(document.createElement("id"));
Node username = server.appendChild(document.createElement("username"));
Node password = server.appendChild(document.createElement("password"));
id.setTextContent("ossrh");
username.setTextContent(System.getenv("OSSRH_USER"));
password.setTextContent(System.getenv("OSSRH_PASSWORD"));
}
public static void profiles(Document document, Node settings) {
Node profiles = hasChild(settings, "profiles");
if (profiles == null) {
profiles = settings.appendChild(document.createElement("profiles"));
}
Node profile = find(profiles, "id", "gpg");
if (profile == null) {
profile = profiles.appendChild(document.createElement("profile"));
}
removeChildren(profile);
Node id = profile.appendChild(document.createElement("id"));
id.setTextContent("gpg");
Node properties = profile.appendChild(document.createElement("properties"));
Node passphrase = properties.appendChild(document.createElement("gpg.passphrase"));
Node publicKeyring = properties.appendChild(document.createElement("gpg.publicKeyring"));
Node secretKeyring = properties.appendChild(document.createElement("gpg.secretKeyring"));
passphrase.setTextContent(System.getenv("OSSRH_PASSWORD"));
publicKeyring.setTextContent(System.getenv("TRAVIS_BUILD_DIR") + "/public.gpg");
secretKeyring.setTextContent(System.getenv("TRAVIS_BUILD_DIR") + "/private.gpg");
}
public static void activeProfiles(Document document, Node settings) {
Node activeProfiles = hasChild(settings, "activeProfiles");
if (activeProfiles == null) {
activeProfiles = settings.appendChild(document.createElement("activeProfiles"));
}
if (hasChildValue(activeProfiles, "gpg") == null) {
Node activeProfile = activeProfiles.appendChild(document.createElement("activeProfile"));
activeProfile.setTextContent("gpg");
}
}
public static Node find(Node node, String name, String value) {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
NodeList values = child.getChildNodes();
for (int x = 0; x < values.getLength(); x++) {
if (child.getNodeName().equals(name) && child.getNodeValue().equals(values)) {
return child;
}
}
}
return null;
}
public static Node hasChild(Node node, String name) {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeName().equals(name)) {
return child;
}
}
return null;
}
public static Node hasChildValue(Node node, String value) {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getTextContent().equals(value)) {
return child;
}
}
return null;
}
public static void removeChildren(Node node) {
while (node.hasChildNodes()) {
node.removeChild(node.getFirstChild());
}
}
}