Check dependency versions for java diagnostics
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2017, 2020 Pivotal, Inc.
|
||||
* Copyright (c) 2017, 2022 Pivotal, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
@@ -21,12 +21,14 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SpringProjectUtil {
|
||||
|
||||
private static final String VERION_PATTERN_STR = "(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?";
|
||||
|
||||
public static final Logger log = LoggerFactory.getLogger(SpringProjectUtil.class);
|
||||
|
||||
private static final Pattern MAJOR_MINOR_VERSION = Pattern.compile("(0|[1-9]\\d*)\\.(0|[1-9]\\d*)");
|
||||
|
||||
// Pattern copied from https://semver.org/
|
||||
private static final Pattern VERSION = Pattern.compile("(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?");
|
||||
private static final Pattern VERSION = Pattern.compile(VERION_PATTERN_STR);
|
||||
|
||||
private static final Pattern SPRING_NAME = Pattern.compile("([a-z]+)(-[a-z]+)*");
|
||||
|
||||
@@ -107,4 +109,38 @@ public class SpringProjectUtil {
|
||||
String name = cpe.getName();
|
||||
return name.startsWith(libNamePrefix) && (!onlyLibs || name.endsWith(".jar"));
|
||||
}
|
||||
|
||||
public static Version getDependencyVersion(IJavaProject jp, String dependency) {
|
||||
try {
|
||||
for (File f : IClasspathUtil.getBinaryRoots(jp.getClasspath(), (cpe) -> !cpe.isSystem())) {
|
||||
String fileName = f.getName();
|
||||
if (fileName.startsWith(dependency)) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append('^');
|
||||
sb.append(dependency);
|
||||
sb.append('-');
|
||||
sb.append(VERION_PATTERN_STR);
|
||||
sb.append(".jar$");
|
||||
Pattern pattern = Pattern.compile(sb.toString());
|
||||
|
||||
Matcher matcher = pattern.matcher(fileName);
|
||||
if (matcher.find() && matcher.groupCount() == 5) {
|
||||
String major = matcher.group(1);
|
||||
String minor = matcher.group(2);
|
||||
String patch = matcher.group(3);
|
||||
String qualifier = matcher.group(4);
|
||||
return new Version(
|
||||
Integer.parseInt(major),
|
||||
Integer.parseInt(minor),
|
||||
Integer.parseInt(patch),
|
||||
qualifier
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2022 VMware, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* https://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* VMware, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.commons.java;
|
||||
|
||||
public final class Version {
|
||||
|
||||
private int major;
|
||||
private int minor;
|
||||
private int patch;
|
||||
private String qualifier;
|
||||
|
||||
public Version(int major, int minor, int patch, String qualifier) {
|
||||
super();
|
||||
this.major = major;
|
||||
this.minor = minor;
|
||||
this.patch = patch;
|
||||
this.qualifier = qualifier;
|
||||
}
|
||||
|
||||
public int getMajor() {
|
||||
return major;
|
||||
}
|
||||
|
||||
public int getMinor() {
|
||||
return minor;
|
||||
}
|
||||
|
||||
public int getPatch() {
|
||||
return patch;
|
||||
}
|
||||
|
||||
public String getQualifier() {
|
||||
return qualifier;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user