1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.plugins.util;
20
21 import java.io.File;
22 import java.io.FileReader;
23 import java.io.IOException;
24
25 import org.xml.sax.Attributes;
26 import org.xml.sax.ContentHandler;
27 import org.xml.sax.InputSource;
28 import org.xml.sax.Locator;
29 import org.xml.sax.SAXException;
30 import org.xml.sax.XMLReader;
31 import org.xml.sax.helpers.XMLReaderFactory;
32
33 public class MavenUtils
34 {
35 private final static String DEFAULT_REPOSITORY_LOCATION = System.getProperty("user.home") + "/.m2/repository/";
36
37
38
39
40
41
42 static File findGlobalSettingsXml()
43 {
44 String m2Home = System.getenv("M2_HOME");
45 return new File(m2Home + "/conf/settings.xml");
46 }
47
48
49
50
51
52
53 static File findUserSettingsXml()
54 {
55 String userHome = System.getProperty("user.home");
56 return new File(userHome + "/.m2/settings.xml");
57 }
58
59
60
61
62
63
64
65 public static String getLocalRepositoryLocation()
66 {
67 String repositoryLocation = DEFAULT_REPOSITORY_LOCATION;
68 try
69 {
70
71 String tempRepositoryLocation = parseSettingsXml(findGlobalSettingsXml());
72 if (tempRepositoryLocation != null)
73 repositoryLocation = tempRepositoryLocation;
74
75
76 tempRepositoryLocation = parseSettingsXml(findUserSettingsXml());
77 if (tempRepositoryLocation != null)
78 repositoryLocation = tempRepositoryLocation;
79 }
80 catch (IOException | SAXException e)
81 {
82 throw new IllegalStateException(e);
83 }
84 return repositoryLocation;
85 }
86
87 private static String parseSettingsXml(File settingsXml) throws IOException, SAXException
88 {
89 if(!settingsXml.exists())
90 return null;
91
92
93 XMLReader reader = XMLReaderFactory.createXMLReader();
94 SettingsXmlContentHandler settingsXmlContentHandler = new SettingsXmlContentHandler();
95 reader.setContentHandler(settingsXmlContentHandler);
96
97 InputSource source = new InputSource(new FileReader(settingsXml));
98 reader.parse(source);
99 return settingsXmlContentHandler.getRepositoryLocation();
100 }
101
102 private static class SettingsXmlContentHandler implements ContentHandler
103 {
104 private String repositoryLocation;
105 private String currentValue;
106
107 @Override
108 public void characters(char[] ch, int start, int length) throws SAXException
109 {
110 currentValue = new String(ch, start, length);
111 }
112
113 @Override
114 public void endElement(String uri, String localName, String qName) throws SAXException
115 {
116 if (localName.equals("localRepository"))
117 {
118 repositoryLocation = currentValue;
119 }
120 }
121
122 public String getRepositoryLocation()
123 {
124 return repositoryLocation;
125 }
126
127 @Override
128 public void setDocumentLocator(Locator locator)
129 {
130 }
131
132 @Override
133 public void startDocument() throws SAXException
134 {
135 }
136
137 @Override
138 public void endDocument() throws SAXException
139 {
140 }
141
142 @Override
143 public void startPrefixMapping(String prefix, String uri) throws SAXException
144 {
145 }
146
147 @Override
148 public void endPrefixMapping(String prefix) throws SAXException
149 {
150 }
151
152 @Override
153 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
154 {
155 }
156
157 @Override
158 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
159 {
160 }
161
162 @Override
163 public void processingInstruction(String target, String data) throws SAXException
164 {
165 }
166
167 @Override
168 public void skippedEntity(String name) throws SAXException
169 {
170 }
171 }
172 }