-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublishScript.gradle
More file actions
59 lines (50 loc) · 2.2 KB
/
publishScript.gradle
File metadata and controls
59 lines (50 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//This script is internal so that the example mod can be published to the maven repository,
//You can delete this script, just make sure to delete the corresponding `apply` line in build.gradle
ext.mavenLocalUrl = repositories.mavenLocal().url.toString()
tasks.register('checkArtifactExists') {
doLast {
if (project.hasProperty('force')) {
logger.lifecycle("Skipping artifact existence check due to --force flag.")
return
}
def repoUrl = project.hasProperty('mavenRepoUrl') ? project.mavenRepoUrl : mavenLocalUrl
def artifactPath = "${repoUrl}/${project.group.replace('.', '/')}/${project.base.archivesName}/${project.version}/${project.base.archivesName}-${project.version}.jar"
logger.lifecycle("Checking if artifact exists at: $artifactPath")
if (artifactPath.startsWith('file:/')) {
// Handle file URLs
def file = new File(new URI(artifactPath))
if (file.exists()) {
throw new IllegalStateException("Artifact '${project.group}:${project.base.archivesName}:${project.version}' already exists. Publishing aborted.")
}
} else {
// Handle HTTP URLs
def url = new URL(artifactPath)
def connection = url.openConnection()
connection.setRequestMethod('HEAD')
if (connection.responseCode == 200) {
throw new IllegalStateException("Artifact '${project.group}:${project.base.archivesName}:${project.version}' already exists. Publishing aborted.")
}
}
logger.lifecycle("Artifact does not exist, proceeding with publish.")
}
}
tasks.named('publish') {
dependsOn 'checkArtifactExists'
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
groupId = project.group
artifactId = project.base.archivesName
version = project.version
// Attach sources JAR to the publication
artifact sourceJar
}
}
repositories {
maven {
url = uri(project.hasProperty('mavenRepoUrl') ? project.mavenRepoUrl : mavenLocalUrl) // Default to mavenLocal if no custom URL is provided
}
}
}