Exemplo n.º 1
0
  public static File findLocalMavenRepository() throws IOException {
    String localRepositoryProperty = System.getProperty("localRepository");
    if (localRepositoryProperty != null) {
      return new File(localRepositoryProperty);
    }

    String homeDir = System.getProperty("user.home");
    File mavenSettingsFile = new File(homeDir + "/.m2/settings.xml");
    if (mavenSettingsFile.exists()) {
      try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(mavenSettingsFile);
        XPath xpath = XPathFactory.newInstance().newXPath();
        SimpleNamespaceContext nc = new SimpleNamespaceContext();
        nc.addPrefix("m", "http://maven.apache.org/POM/4.0.0");
        xpath.setNamespaceContext(nc);

        String localRepository = xpath.evaluate("string(/m:settings/m:localRepository)", document);
        if (localRepository != null && localRepository.length() > 0) {
          return new File(localRepository);
        }

        // Usage of the POM namespace in settings.xml is optional, so also try without namespace
        localRepository = xpath.evaluate("string(/settings/localRepository)", document);
        if (localRepository != null && localRepository.length() > 0) {
          return new File(localRepository);
        }
      } catch (Exception e) {
        throw new IOException(
            "Error reading Maven settings file at " + mavenSettingsFile.getAbsolutePath(), e);
      }
    }
    return new File(homeDir + "/.m2/repository");
  }
Exemplo n.º 2
0
  public void startPrefixMapping(String prefix, String uri) throws SAXException {

    if (prefix == null) {

      prefix = "";

    } else if (prefix.equals("xml")) {

      return;
    }

    if (namespaces == null) {

      namespaces = new SimpleNamespaceContext();
    }
    namespaces.setPrefix(prefix, uri);
  }