View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
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       * Looks for maven's settings.xml in $M2_HOME/conf/settings.xml
39       *
40       * @return a file representing the global settings.xml
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       * Looks for maven's settings.xml in ${user.home}/.m2/settings.xml
50       *
51       * @return a file representing the user's settings.xml
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       * Read the local repository location from settings.xml. A setting in the user's settings.xml will override the
61       * global one.
62       *
63       * @return location of the local maven repo
64       */
65      public static String getLocalRepositoryLocation()
66      {
67          String repositoryLocation = DEFAULT_REPOSITORY_LOCATION;
68          try
69          {
70              // find the global setting
71              String tempRepositoryLocation = parseSettingsXml(findGlobalSettingsXml());
72              if (tempRepositoryLocation != null)
73                  repositoryLocation = tempRepositoryLocation;
74  
75              // override with user settings.xml
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          // readability is more important than efficiency here, so we just recreate those objects
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 }