Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 87339bd3d6a59a13832eabcd888e12c4b0dc8f11 (plain) (blame)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//========================================================================
//Copyright 2012 Mort Bay Consulting Pty. Ltd.
//------------------------------------------------------------------------
//All rights reserved. This program and the accompanying materials
//are made available under the terms of the Eclipse Public License v1.0
//and Apache License v2.0 which accompanies this distribution.
//The Eclipse Public License is available at
//http://www.eclipse.org/legal/epl-v10.html
//The Apache License v2.0 is available at
//http://www.opensource.org/licenses/apache2.0.php
//You may elect to redistribute this code under either of these licenses.
//========================================================================
package org.eclipse.jetty.plugins.util;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class MavenUtils
{
    private final static String DEFAULT_REPOSITORY_LOCATION = System.getProperty("user.home") + "/.m2/repository/";

    /**
     * Looks for maven's settings.xml in $M2_HOME/conf/settings.xml
     *
     * @return a file representing the global settings.xml
     */
    static File findGlobalSettingsXml()
    {
        String m2Home = System.getenv("M2_HOME");
        return new File(m2Home + "/conf/settings.xml");
    }

    /**
     * Looks for maven's settings.xml in ${user.home}/.m2/settings.xml
     *
     * @return a file representing the user's settings.xml
     */
    static File findUserSettingsXml()
    {
        String userHome = System.getProperty("user.home");
        return new File(userHome + "/.m2/settings.xml");
    }

    /**
     * Read the local repository location from settings.xml. A setting in the user's settings.xml will override the
     * global one.
     *
     * @return location of the local maven repo
     */
    public static String getLocalRepositoryLocation()
    {
        String repositoryLocation = DEFAULT_REPOSITORY_LOCATION;
        try
        {
            // find the global setting
            String tempRepositoryLocation = parseSettingsXml(findGlobalSettingsXml());
            if (tempRepositoryLocation != null)
                repositoryLocation = tempRepositoryLocation;

            // override with user settings.xml
            tempRepositoryLocation = parseSettingsXml(findUserSettingsXml());
            if (tempRepositoryLocation != null)
                repositoryLocation = tempRepositoryLocation;
        }
        catch (IOException | SAXException e)
        {
            throw new IllegalStateException(e);
        }
        return repositoryLocation;
    }

    private static String parseSettingsXml(File settingsXml) throws IOException, SAXException
    {
        if(!settingsXml.exists())
            return null;

        // readability is more important than efficiency here, so we just recreate those objects
        XMLReader reader = XMLReaderFactory.createXMLReader();
        SettingsXmlContentHandler settingsXmlContentHandler = new SettingsXmlContentHandler();
        reader.setContentHandler(settingsXmlContentHandler);

        InputSource source = new InputSource(new FileReader(settingsXml));
        reader.parse(source);
        return settingsXmlContentHandler.getRepositoryLocation();
    }

    private static class SettingsXmlContentHandler implements ContentHandler
    {
        private String repositoryLocation;
        private String currentValue;

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException
        {
            currentValue = new String(ch, start, length);
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException
        {
            if (localName.equals("localRepository"))
            {
                repositoryLocation = currentValue;
            }
        }

        public String getRepositoryLocation()
        {
            return repositoryLocation;
        }

        @Override
        public void setDocumentLocator(Locator locator)
        {
        }

        @Override
        public void startDocument() throws SAXException
        {
        }

        @Override
        public void endDocument() throws SAXException
        {
        }

        @Override
        public void startPrefixMapping(String prefix, String uri) throws SAXException
        {
        }

        @Override
        public void endPrefixMapping(String prefix) throws SAXException
        {
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
        {
        }

        @Override
        public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
        {
        }

        @Override
        public void processingInstruction(String target, String data) throws SAXException
        {
        }

        @Override
        public void skippedEntity(String name) throws SAXException
        {
        }
    }
}

Back to the top