0

When I use the gradle plug-in maven pbulish to upload jars to the Maven repository, how can I exclude some dependencies in the gradle project? My gradle. Build file is as follows:

dependencies {
    compile project(":frame:f_frame");
    compile project(":p_AppComm");
    compile project(":p_Systemaudit");
}

publishing {
    repositories {
        maven {
            url = 'http://XXX/repository/infra_test_snapshot/'

            credentials {
                username = 'admin'
                password = 'admin'
            }
        }
    }

    publications {
        maven(MavenPublication) {
            groupId "AAAAA"
            artifactId "BBBBB"
            version "CCCCC"
            from components.java

            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')
                configurations.implementation.allDependencies.each {
                    if (it.name != null && !"unspecified".equals(it.name) && !"f_frame".equals(it.name) && !"p_AppComm".equals(it.name)) {
                        println it.toString()
                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', it.group)
                        dependencyNode.appendNode('artifactId', it.name)
                        dependencyNode.appendNode('version', it.version)
                        dependencyNode.appendNode('scope', 'implementation')
                    }
                }
            }

            artifact sourceJar {
                classifier "sources"
            }
        }
    }
}

How can I modify the contents of the pom.withxml function to remove the f_frame and p_Appcomm in the POM file dependency in the Maven repository.

2 Answers2

0
  1. dependencyNode.appendNode('scope', 'implementation'). maven does not have any 'implementation' scope, valid values are - compile, provided, runtime, test

  2. print all the names / types of the dependencies in the dependencies loop - exclude all 'project' type dependencies - like your if condition

PrasadU
  • 2,154
  • 1
  • 9
  • 10
  • I'm not sure what to do with the second point....I modified the problem description. I hope to keep only ```compile project(":p_Systemaudit");``` dependencies. – keshuan_jeme Oct 27 '21 at 05:20
  • I wonder if the ```ac_systemaudit``` dependency in the POM file automatically generated by Maven publish conflicts with the ```p_Systemaudit``` dependency I added manually. – keshuan_jeme Oct 27 '21 at 05:41
0

When publishing jar packages using maven-publish plug-in, dependencies have been added to the automatically generated POM file according to dependencies{}. I shouldn't add the same dependencies again. The correct way is to cycle the asNode() and remove the parts I don't need.

The modified code is as follows:

pom.withXml {
                Node pom = asNode()
                NodeList pomNodes = pom.value()
                Node dependencies = pomNodes.get(4)
                NodeList pomDependencies = dependencies.value()

                Iterator iterator = pomDependencies.iterator();
                while (iterator.hasNext()) {
                    Node dependeny = iterator.next()
                    NodeList str = dependeny.get("artifactId")
                    String dependName = str.get(0).value().get(0)
                    if ("f_frame".equals(dependName) || "p_AppComm".equals(dependName)) {
                        iterator.remove()
                    }
                }
            }