1
1
package taboolib .common .env ;
2
2
3
+ import org .jetbrains .annotations .Nullable ;
3
4
import org .w3c .dom .Element ;
4
5
5
6
import java .io .File ;
7
+ import java .io .IOException ;
8
+ import java .net .MalformedURLException ;
9
+ import java .net .URL ;
6
10
import java .text .ParseException ;
11
+ import java .util .Collection ;
7
12
import java .util .Objects ;
8
13
9
14
/**
16
21
public class Dependency extends AbstractXmlParser {
17
22
18
23
/**
19
- * A placeholder string for when the version has not been specified
20
- *
21
- * @since 1.0.0
24
+ * 当版本尚未指定时的占位符字符串
22
25
*/
23
26
private static final String LATEST_VERSION = "latest" ;
24
27
25
28
/**
26
- * The ID of the group for this dependency
27
- *
28
- * @since 1.0.0
29
+ * 此依赖项的组 ID
29
30
*/
30
31
private final String groupId ;
31
32
32
33
/**
33
- * The ID of the artifact for this dependency
34
- *
35
- * @since 1.0.0
34
+ * 此依赖项的工件 ID
36
35
*/
37
36
private final String artifactId ;
37
+
38
38
/**
39
- * The scope of the dependency
40
- *
41
- * @since 1.0.0
39
+ * 依赖项的范围
42
40
*/
43
41
private final DependencyScope scope ;
42
+
44
43
/**
45
- * The version of the artifact to download, or
46
- * {@link Dependency#LATEST_VERSION} if it is not specified in the pom
47
- *
48
- * @since 1.0.0
44
+ * 要下载的版本,或者如果在 pom 中没有指定依赖项的最新版本,则设置依赖项的最新版本
49
45
*/
50
46
private String version ;
51
47
52
- /**
53
- * Creates a new dependency
54
- *
55
- * @param groupId The group ID
56
- * @param artifactId The artifact ID
57
- * @param version The version to download
58
- * @param scope The scope
59
- * @since 1.0.0
60
- */
61
48
public Dependency (String groupId , String artifactId , String version , DependencyScope scope ) {
62
49
this .groupId = groupId ;
63
50
this .artifactId = artifactId ;
64
51
this .version = version .contains ("$" ) || version .contains ("[" ) || version .contains ("(" ) ? LATEST_VERSION : version ;
65
52
this .scope = scope ;
66
53
}
67
54
68
- /**
69
- * Creates a new dependency from the specified element in the pom
70
- *
71
- * @param node The element to create the dependency from
72
- * @throws ParseException If the xml could not be parsed
73
- * @since 1.0.0
74
- */
75
55
public Dependency (Element node ) throws ParseException {
76
56
this (find ("groupId" , node ), find ("artifactId" , node ), find ("version" , node , LATEST_VERSION ), DependencyScope .valueOf (find ("scope" , node , "runtime" ).toUpperCase ()));
77
57
}
78
58
79
59
/**
80
- * Gets the ID of the group for this dependency
81
- *
82
- * @return The group ID
83
- * @since 1.0.0
60
+ * 获取依赖的下载地址
84
61
*/
85
- public String getGroupId () {
86
- return groupId ;
62
+ public URL getURL (Repository repo , String ext ) throws MalformedURLException {
63
+ String name = String .format ("%s-%s.%s" , getArtifactId (), getVersion (), ext );
64
+ return new URL (String .format ("%s/%s/%s/%s/%s" , repo .getUrl (), getGroupId ().replace ('.' , '/' ), getArtifactId (), getVersion (), name ));
87
65
}
88
66
89
67
/**
90
- * Gets the ID of the artifact for this dependency
91
- *
92
- * @return The artifact ID
93
- * @since 1.0.0
68
+ * 检查依赖项的版本
69
+ * 如果版本尚未指定,则尝试从仓库中获取最新版本
94
70
*/
95
- public String getArtifactId () {
96
- return artifactId ;
97
- }
98
-
99
- /**
100
- * Gets the version of the artifact to download
101
- *
102
- * @return The version, or <code>null</code> if the latest version should be
103
- * downloaded and the latest version has not been determined yet
104
- * @since 1.0.0
105
- */
106
- public String getVersion () {
107
- return version .equals (LATEST_VERSION ) ? null : version ;
71
+ public void checkVersion (Collection <Repository > repositories , File baseDir ) throws IOException {
72
+ if (getVersion () == null ) {
73
+ // 获取本地最新版本
74
+ DependencyVersion installedLatestVersion = getInstalledLatestVersion (baseDir );
75
+ // 是否检查更新
76
+ boolean checkUpdate = false ;
77
+ // 本地版本不存在
78
+ if (installedLatestVersion == null ) {
79
+ checkUpdate = true ;
80
+ }
81
+ // 2022/3/31
82
+ // HikariCP 引用的 slf4j 为 latest 版本,因此每次开服都会尝试从仓库中获取最新版本
83
+ else if (VersionChecker .isOutdated ()) {
84
+ checkUpdate = true ;
85
+ VersionChecker .updateCheckTime ();
86
+ }
87
+ IOException e = null ;
88
+ if (checkUpdate ) {
89
+ // 尝试从仓库中获取最新版本
90
+ for (Repository repo : repositories ) {
91
+ try {
92
+ repo .getLatestVersion (this );
93
+ e = null ;
94
+ break ;
95
+ } catch (IOException ex ) {
96
+ e = new IOException (String .format ("Unable to find latest version of %s" , this ), ex );
97
+ }
98
+ }
99
+ if (e != null ) {
100
+ throw e ;
101
+ }
102
+ } else {
103
+ setVersion (installedLatestVersion .toString ());
104
+ }
105
+ }
108
106
}
109
107
110
108
/**
111
- * Sets the latest version of this dependency
112
- *
113
- * @param version The latest version
114
- * @throws IllegalStateException If this dependency has a specific version already
115
- * @since 1.0.0
109
+ * Get the latest version of this artifact that are currently
110
+ * downloaded on this computer
116
111
*/
117
- public void setVersion ( String version ) {
118
- if (! this . version . equals ( LATEST_VERSION ) ) {
119
- throw new IllegalStateException ( "Version is already resolved" ) ;
120
- } else if ( version . equals ( LATEST_VERSION )) {
121
- throw new IllegalArgumentException ( "Cannot set version to the latest" );
122
- } else {
123
- this . version = version ;
112
+ @ Nullable
113
+ public DependencyVersion getInstalledLatestVersion ( File baseDir ) {
114
+ DependencyVersion max = null ;
115
+ for ( DependencyVersion ver : getInstalledVersions ( baseDir )) {
116
+ if ( max == null || ver . compareTo ( max ) > 0 ) {
117
+ max = ver ;
118
+ }
124
119
}
120
+ return max ;
125
121
}
126
122
127
123
/**
128
- * Gets a list of all of the versions of this artifact that are currently
124
+ * Gets a list of all the versions of this artifact that are currently
129
125
* downloaded on this computer
130
126
*
131
- * @param dir The directory to store downloaded artifacts in
132
127
* @return An array of the versions that are already downloaded
133
- * @since 1.0.0
134
128
*/
135
129
public DependencyVersion [] getInstalledVersions (File dir ) {
136
130
for (String part : getGroupId ().split ("\\ ." )) {
@@ -148,26 +142,17 @@ public DependencyVersion[] getInstalledVersions(File dir) {
148
142
return versions ;
149
143
}
150
144
151
- /**
152
- * Gets the scope of this dependency
153
- *
154
- * @return The scope
155
- * @since 1.0.0
156
- */
157
- public DependencyScope getScope () {
158
- return scope ;
159
- }
160
-
161
145
/**
162
146
* Gets the file that the downloaded artifact should be stored in
163
147
*
164
148
* @param dir The directory to store downloaded artifacts in
165
- * @param ext The file extension to download (should be either
166
- * <code>"jar"</code> or <code>"pom"</code>
149
+ * @param ext The file extension to download (should be either <code>"jar"</code> or <code>"pom"</code>)
167
150
* @return The file to download into
168
- * @since 1.0.0
169
151
*/
170
- public File getFile (File dir , String ext ) {
152
+ public File findFile (File dir , String ext ) {
153
+ if (getVersion () == null ) {
154
+ throw new IllegalStateException ("Version is not resolved: " + this );
155
+ }
171
156
for (String part : getGroupId ().split ("\\ ." )) {
172
157
dir = new File (dir , part );
173
158
}
@@ -177,6 +162,35 @@ public File getFile(File dir, String ext) {
177
162
return dir ;
178
163
}
179
164
165
+ /**
166
+ * Sets the version of this dependency
167
+ */
168
+ public void setVersion (String version ) {
169
+ if (!this .version .equals (LATEST_VERSION )) {
170
+ throw new IllegalStateException ("Version is already resolved" );
171
+ } else if (version .equals (LATEST_VERSION )) {
172
+ throw new IllegalArgumentException ("Cannot set version to the latest" );
173
+ } else {
174
+ this .version = version ;
175
+ }
176
+ }
177
+
178
+ public String getGroupId () {
179
+ return groupId ;
180
+ }
181
+
182
+ public String getArtifactId () {
183
+ return artifactId ;
184
+ }
185
+
186
+ public String getVersion () {
187
+ return version .equals (LATEST_VERSION ) ? null : version ;
188
+ }
189
+
190
+ public DependencyScope getScope () {
191
+ return scope ;
192
+ }
193
+
180
194
@ Override
181
195
public String toString () {
182
196
return String .format ("%s:%s:%s" , groupId , artifactId , version );
0 commit comments