提交 823c745e 编写于 作者: J jurgen

Maven profiles

Former-commit-id: 3aa9d505
上级 52730f25
......@@ -320,7 +320,7 @@ public class MavenArtifact
return localVersion;
}
private boolean versionMatches(String version, String versionSpec) {
public static boolean versionMatches(String version, String versionSpec) {
try {
if (versionSpec.startsWith("{") && versionSpec.endsWith("}")) {
Pattern versionPattern = Pattern.compile(versionSpec.substring(1, versionSpec.length() - 1));
......
......@@ -32,7 +32,9 @@ import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* Maven artifact version descriptor (POM).
......@@ -50,16 +52,18 @@ public class MavenArtifactVersion {
private String description;
private String url;
private MavenArtifactVersion parent;
private Map<String, String> properties = new LinkedHashMap<>();
private List<MavenArtifactLicense> licenses = new ArrayList<>();
private List<MavenArtifactDependency> dependencies;
private List<MavenArtifactDependency> dependencyManagement;
private final List<MavenArtifactLicense> licenses = new ArrayList<>();
private final List<MavenProfile> profiles = new ArrayList<>();
private GeneralUtils.IVariableResolver propertyResolver = new GeneralUtils.IVariableResolver() {
@Override
public String get(String name) {
for (MavenArtifactVersion v = MavenArtifactVersion.this; v != null; v = v.parent) {
String value = v.properties.get(name);
for (MavenProfile profile : profiles) {
if (!profile.isActive()) {
continue;
}
String value = profile.properties.get(name);
if (value != null) {
return value;
} else if (name.equals(PROP_PROJECT_VERSION)) {
......@@ -70,6 +74,7 @@ public class MavenArtifactVersion {
return v.artifact.getArtifactId();
}
}
}
return null;
}
};
......@@ -104,35 +109,26 @@ public class MavenArtifactVersion {
return parent;
}
public Map<String, String> getProperties() {
return properties;
}
public List<MavenArtifactLicense> getLicenses() {
return licenses;
}
public List<MavenArtifactDependency> getDependencies(DBRProgressMonitor monitor) {
List<MavenArtifactDependency> dependencies = new ArrayList<>();
for (MavenProfile profile : profiles) {
if (profile.isActive() && !CommonUtils.isEmpty(profile.dependencies)) {
dependencies.addAll(profile.dependencies);
}
}
if (parent != null) {
List<MavenArtifactDependency> parentDependencies = parent.getDependencies(monitor);
if (!CommonUtils.isEmpty(parentDependencies)) {
if (CommonUtils.isEmpty(dependencies)) {
return parentDependencies;
}
List<MavenArtifactDependency> result = new ArrayList<>(dependencies.size() + parentDependencies.size());
result.addAll(dependencies);
result.addAll(parentDependencies);
return result;
dependencies.addAll(parentDependencies);
}
}
return this.dependencies;
}
List<MavenArtifactDependency> getDependencies() {
return dependencies;
}
public File getCacheFile() {
if (artifact.getRepository().isLocal()) {
String externalURL = getExternalURL(MavenArtifact.FILE_JAR);
......@@ -221,15 +217,6 @@ public class MavenArtifactVersion {
}
}
{
// Properties
Element propsElement = XMLUtils.getChildElement(root, "properties");
if (propsElement != null) {
for (Element prop : XMLUtils.getChildElementList(propsElement)) {
properties.put(prop.getTagName(), XMLUtils.getElementBody(prop));
}
}
}
{
// Licenses
Element licensesElement = XMLUtils.getChildElement(root, "licenses");
......@@ -242,15 +229,58 @@ public class MavenArtifactVersion {
}
}
}
// Default profile
MavenProfile defaultProfile = new MavenProfile(null);
parseProfile(monitor, defaultProfile, root);
profiles.add(defaultProfile);
{
// Profiles
Element licensesElement = XMLUtils.getChildElement(root, "profiles");
if (licensesElement != null) {
for (Element profElement : XMLUtils.getChildElementList(licensesElement, "profile")) {
MavenProfile profile = new MavenProfile(XMLUtils.getChildElementBody(profElement, "id"));
parseProfile(monitor, profile, profElement);
}
}
}
monitor.worked(1);
}
private void parseProfile(DBRProgressMonitor monitor, MavenProfile profile, Element element) {
{
// Activation
Element activationElement = XMLUtils.getChildElement(element, "activation");
if (activationElement != null) {
String activeByDefault = XMLUtils.getChildElementBody(element, "activeByDefault");
if (!CommonUtils.isEmpty(activeByDefault)) {
profile.active = CommonUtils.getBoolean(activeByDefault);
}
String jdk = XMLUtils.getChildElementBody(element, "jdk");
if (!CommonUtils.isEmpty(jdk)) {
profile.active = MavenArtifact.versionMatches(System.getProperty("java.version"), jdk);
}
}
}
{
// Properties
Element propsElement = XMLUtils.getChildElement(element, "properties");
if (propsElement != null) {
for (Element prop : XMLUtils.getChildElementList(propsElement)) {
profile.properties.put(prop.getTagName(), XMLUtils.getElementBody(prop));
}
}
}
{
// Dependencies
Element dmElement = XMLUtils.getChildElement(root, "dependencyManagement");
Element dmElement = XMLUtils.getChildElement(element, "dependencyManagement");
if (dmElement != null) {
dependencyManagement = parseDependencies(monitor, dmElement, true);
profile.dependencyManagement = parseDependencies(monitor, dmElement, true);
}
dependencies = parseDependencies(monitor, root, false);
profile.dependencies = parseDependencies(monitor, element, false);
}
monitor.worked(1);
}
private void cachePOM(File localPOM) throws IOException {
......@@ -335,15 +365,16 @@ public class MavenArtifactVersion {
}
private String findDependencyVersion(DBRProgressMonitor monitor, String groupId, String artifactId) {
if (dependencyManagement != null) {
for (MavenArtifactDependency dmArtifact : dependencyManagement) {
for (MavenProfile profile : profiles) {
if (profile.isActive() && profile.dependencyManagement != null) {
for (MavenArtifactDependency dmArtifact : profile.dependencyManagement) {
if (dmArtifact.getGroupId().equals(groupId) &&
dmArtifact.getArtifactId().equals(artifactId))
{
dmArtifact.getArtifactId().equals(artifactId)) {
return dmArtifact.getVersion();
}
}
}
}
return parent == null ? null : parent.findDependencyVersion(monitor, groupId, artifactId);
}
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2015 Serge Rieder (serge@jkiss.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License (version 2)
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.jkiss.dbeaver.registry.maven;
import org.w3c.dom.Element;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Maven build profile
*/
public class MavenProfile {
private final String id;
Map<String, String> properties = new LinkedHashMap<>();
List<MavenArtifactDependency> dependencies;
List<MavenArtifactDependency> dependencyManagement;
boolean active;
public MavenProfile(String id) {
this.id = id;
}
public String getId() {
return id;
}
public Map<String, String> getProperties() {
return properties;
}
public List<MavenArtifactDependency> getDependencies() {
return dependencies;
}
public List<MavenArtifactDependency> getDependencyManagement() {
return dependencyManagement;
}
public boolean isActive() {
return active;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册