Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions common-env/src/main/java/taboolib/common/env/ParsedDependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.tabooproject.reflex.LazyEnum;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -62,7 +63,12 @@ public class ParsedDependency {
*/
private final boolean external;

public ParsedDependency(String value, String test, String repository, boolean transitive, boolean ignoreOptional, boolean ignoreException, List<DependencyScope> scopes, List<String> relocate, boolean external) {
/**
* 过滤库
*/
private final List<String> excludes;

public ParsedDependency(String value, String test, String repository, boolean transitive, boolean ignoreOptional, boolean ignoreException, List<DependencyScope> scopes, List<String> relocate, boolean external, List<String> excludes) {
this.value = value;
this.test = test;
this.repository = repository;
Expand All @@ -72,6 +78,7 @@ public ParsedDependency(String value, String test, String repository, boolean tr
this.scopes = scopes;
this.relocate = relocate;
this.external = external;
this.excludes = excludes;
}

@SuppressWarnings("unchecked")
Expand All @@ -84,9 +91,10 @@ public ParsedDependency(Map<String, Object> map) {
this.ignoreException = (boolean) map.getOrDefault("ignoreException", false);
this.relocate = (List<String>) map.getOrDefault("relocate", new ArrayList<>());
this.external = (boolean) map.getOrDefault("external", true);
this.scopes = new ArrayList<>();
this.scopes = new ArrayList<>(Arrays.asList(DependencyScope.RUNTIME, DependencyScope.COMPILE));
List<LazyEnum> scopesEnums = (List<LazyEnum>) map.getOrDefault("scopes", new ArrayList<>());
scopesEnums.forEach(it -> this.scopes.add((DependencyScope) it.getInstance()));
this.excludes = (List<String>) map.getOrDefault("excludes", new ArrayList<>());
}

public String value() {
Expand Down Expand Up @@ -125,6 +133,10 @@ public boolean external() {
return external;
}

public List<String> excludes() {
return excludes;
}

@Override
public String toString() {
return "ParsedDependency{" +
Expand All @@ -137,6 +149,7 @@ public String toString() {
", scopes=" + scopes +
", relocate=" + relocate +
", external=" + external +
", exclude =" + excludes +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@
* 是否外部库(不会被扫到)
*/
boolean external() default true;

/**
* 过滤指定模块
*/
String[] excludes() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public int loadDependency(@NotNull ReflexClass clazz) throws Throwable {
relocation.add(new JarRelocation(from, to));
}
String url = dep.value().startsWith("!") ? dep.value().substring(1) : dep.value();
loadDependency(url, baseFile, relocation, dep.repository(), dep.ignoreOptional(), dep.ignoreException(), dep.transitive(), dep.scopes(), dep.external());
loadDependency(url, baseFile, relocation, dep.repository(), dep.ignoreOptional(), dep.ignoreException(), dep.transitive(), dep.scopes(), dep.external(), dep.excludes());
}
}
return total;
Expand Down Expand Up @@ -130,30 +130,15 @@ public void loadDependency(@NotNull String url, @NotNull File baseDir, @Nullable
loadDependency(url, baseDir, new ArrayList<>(), repository, true, false, true, Arrays.asList(DependencyScope.RUNTIME, DependencyScope.COMPILE));
}

public void loadDependency(
@NotNull String url,
@NotNull File baseDir,
@NotNull List<JarRelocation> relocation,
@Nullable String repository,
boolean ignoreOptional,
boolean ignoreException,
boolean transitive,
@NotNull List<DependencyScope> scope
) throws Throwable {
public void loadDependency(@NotNull String url, @NotNull File baseDir, @NotNull List<JarRelocation> relocation, @Nullable String repository, boolean ignoreOptional, boolean ignoreException, boolean transitive, @NotNull List<DependencyScope> scope) throws Throwable {
loadDependency(url, baseDir, relocation, repository, ignoreOptional, ignoreException, transitive, scope, true);
}

public void loadDependency(
@NotNull String url,
@NotNull File baseDir,
@NotNull List<JarRelocation> relocation,
@Nullable String repository,
boolean ignoreOptional,
boolean ignoreException,
boolean transitive,
@NotNull List<DependencyScope> scope,
boolean external
) throws Throwable {
public void loadDependency(@NotNull String url, @NotNull File baseDir, @NotNull List<JarRelocation> relocation, @Nullable String repository, boolean ignoreOptional, boolean ignoreException, boolean transitive, @NotNull List<DependencyScope> scope, boolean external) throws Throwable {
loadDependency(url, baseDir, relocation, repository, ignoreOptional, ignoreException, transitive, scope, external, new ArrayList<>());
}

public void loadDependency(@NotNull String url, @NotNull File baseDir, @NotNull List<JarRelocation> relocation, @Nullable String repository, boolean ignoreOptional, boolean ignoreException, boolean transitive, @NotNull List<DependencyScope> scope, boolean external, @NotNull List<String> excludes) throws Throwable {
// 支持用户对源进行替换
if (repository == null || repository.isEmpty()) {
repository = defaultRepositoryCentral;
Expand All @@ -170,50 +155,28 @@ public void loadDependency(
}
});
} else {
loadDependencyLegacy(url, baseDir, relocation, repository, ignoreOptional, ignoreException, transitive, scope, external);
loadDependencyLegacy(url, baseDir, relocation, repository, ignoreOptional, ignoreException, transitive, scope, external, excludes);
}
}

void loadDependencyLegacy(
@NotNull String url,
@NotNull File baseDir,
@NotNull List<JarRelocation> relocation,
String repository,
boolean ignoreOptional,
boolean ignoreException,
boolean transitive,
@NotNull List<DependencyScope> scope,
boolean external
) throws Throwable {
void loadDependencyLegacy(@NotNull String url, @NotNull File baseDir, @NotNull List<JarRelocation> relocation, String repository, boolean ignoreOptional, boolean ignoreException, boolean transitive, @NotNull List<DependencyScope> scope, boolean external, @NotNull List<String> excludes) throws Throwable {
Artifact artifact = new Artifact(url);
DependencyDownloader downloader = new DependencyDownloader(baseDir, relocation);
downloader.addRepository(new Repository(repository));
downloader.setIgnoreOptional(ignoreOptional);
downloader.setIgnoreException(ignoreException);
downloader.setDependencyScopes(scope);
downloader.setTransitive(transitive);
downloader.setExcludes(excludes);
// 解析依赖
String pomPath = String.format(
"%s/%s/%s/%s-%s.pom",
artifact.getGroupId().replace('.', '/'),
artifact.getArtifactId(),
artifact.getVersion(),
artifact.getArtifactId(),
artifact.getVersion()
);
String pomPath = String.format("%s/%s/%s/%s-%s.pom", artifact.getGroupId().replace('.', '/'), artifact.getArtifactId(), artifact.getVersion(), artifact.getArtifactId(), artifact.getVersion());
File pomFile = new File(baseDir, pomPath);
File pomFile1 = new File(pomFile.getPath() + ".sha1");
// 验证文件完整性
if (PrimitiveIO.validation(pomFile, pomFile1)) {
downloader.loadDependencyFromInputStream(pomFile.toPath().toUri().toURL().openStream());
} else {
PrimitiveIO.println(
t("正在下载依赖 {0}:{1}:{2} {3}", "Downloading library {0}:{1}:{2} {3}"),
artifact.getGroupId(),
artifact.getArtifactId(),
artifact.getVersion(),
transitive ? t("(传递模式)", "(transitive)") : ""
);
PrimitiveIO.println(t("正在下载依赖 {0}:{1}:{2} {3}", "Downloading library {0}:{1}:{2} {3}"), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), transitive ? t("(传递模式)", "(transitive)") : "");
downloader.loadDependencyFromInputStream(new URL(repository + "/" + pomPath).openStream());
}
// 加载自身
Expand Down Expand Up @@ -276,8 +239,10 @@ public void loadFromJson(String json) throws Throwable {
for (int i = 0; i + 1 < relocate.size(); i += 2) {
relocation.add(new JarRelocation(relocate.get(i).getAsString(), relocate.get(i + 1).getAsString()));
}
List<String> excludes = new ArrayList<>();
array(object, "excludes").forEach((e) -> excludes.add(e.getAsString()));
// 加载依赖
loadDependency(value, new File(defaultLibrary), relocation, repository, ignoreOptional, ignoreException, transitive, scopes, external);
loadDependency(value, new File(defaultLibrary), relocation, repository, ignoreOptional, ignoreException, transitive, scopes, external, excludes);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public class DependencyDownloader extends AbstractXmlParser {
*/
private boolean isTransitive = true;

/**
* 过滤指定内容
*/
private List<String> excludes = new ArrayList<>();

public DependencyDownloader(@Nullable File baseDir) {
this.baseDir = baseDir;
}
Expand Down Expand Up @@ -182,6 +187,9 @@ public Set<Dependency> loadDependency(Collection<Repository> repositories, Depen
singleton.add(dependency);
return singleton;
}
if (excludes.contains(dependency.getGroupId() + ":" + dependency.getArtifactId())) {
return new HashSet<>();
}
// 获取依赖项的 pom 文件和 jar 文件
File pom = dependency.findFile(baseDir, "pom");
File pom1 = new File(pom.getPath() + ".sha1");
Expand Down Expand Up @@ -358,4 +366,12 @@ public boolean isTransitive() {
public void setTransitive(boolean transitive) {
isTransitive = transitive;
}

public List<String> getExcludes() {
return excludes;
}

public void setExcludes(List<String> excludes) {
this.excludes = excludes;
}
}
Loading