I'm changing build systems from maven to gradle for a spring boot project. I get this stacktrace
19:03:08: Executing external task 'bootRun'...
/home/dac/proj/spring-boot-master/spring-boot-samples/spring-boot-sample-jetty-jsp/src/main/java/sample/jetty/jsp/SampleJettyJspApplication.java:22: error: package org.springframework.boot.web.support does not exist
import org.springframework.boot.web.support.SpringBootServletInitializer;
^
/home/dac/proj/spring-boot-master/spring-boot-samples/spring-boot-sample-jetty-jsp/src/main/java/sample/jetty/jsp/SampleJettyJspApplication.java:25: error: cannot find symbol
public class SampleJettyJspApplication extends SpringBootServletInitializer {
^
symbol: class SpringBootServletInitializer
/home/dac/proj/spring-boot-master/spring-boot-samples/spring-boot-sample-jetty-jsp/src/main/java/sample/jetty/jsp/SampleJettyJspApplication.java:27: error: method does not override or implement a method from a supertype
#Override
^
3 errors
:compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 2.334 secs
Compilation failed; see the compiler error output for details.
19:03:10: External task execution finished 'bootRun'.
My build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE"
classpath 'org.springframework:springloaded:1.2.5.RELEASE'
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'rebel'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.zeroturnaround', name: 'gradle-jrebel-plugin', version: '1.1.3'
}
}
jar {
baseName = 'gs-spring-boot'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
// tag::jetty[]
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
// end::jetty[]
// tag::actuator[]
compile("org.springframework.boot:spring-boot-starter-actuator")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("junit:junit")
}
// change default IntelliJ output directory for compiling classes
idea {
module {
inheritOutputDirs = false
outputDir = file("$buildDir/classes/main/")
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
My pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-samples</artifactId>
<version>1.4.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-sample-jetty-jsp</artifactId>
<packaging>war</packaging>
<name>Spring Boot Jetty JSP Sample</name>
<description>Spring Boot Jetty JSP Sample</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>apache-jsp</artifactId>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
How should I make the package available to gradle?
You are using org.springframework.boot.context.web.SpringBootServletInitializer this is deprecated. Instead:
Use
org.springframework.boot.web.support.SpringBootServletInitializer
For SpringBoot 2.0
org.springframework.boot.web.servlet.support.SpringBootServletInitializer
It's probably an import issue in your source code - your Gradle build script uses Spring Boot 1.3.6.RELEASE in which SpringBootServletInitializer has the following fully qualified name:
org.springframework.boot.context.web.SpringBootServletInitializer
Your Maven pom.xml, however, uses Spring Boot 1.4.0.BUILD-SNAPSHOT, in which the package name was changed to:
org.springframework.boot.web.support.SpringBootServletInitializer
So if you go to your SampleJettyJspApplication and change the import to
import org.springframework.boot.context.web.SpringBootServletInitializer;
everything should be fine.
Alternatively, you could alter your Gradle build script to import 1.4.0.BUILD-SNAPSHOT, but that would require adding Spring's snapshot repository:
buildscript {
repositories {
maven.url "http://repo.spring.io/snapshot"
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.BUILD-SNAPSHOT")
}
}
You should use the same spring boot version 1.4.0.BUILD-SNAPSHOT as in maven. org.springframework.boot.web.support.SpringBootServletInitializer was introduced since 1.4.0 that's why gradle cannot find it.
org.springframework.boot.web.support.SpringBootServletInitializer
is deprecated.
use below instead:
org.springframework.boot.web.servlet.support.SpringBootServletInitializer
Related
Say I want to retrieve with graddle the dependancy tree of this artifact : com.google.firebase:firebase-firestore:24.4.0
How can I do ?
You can't do that, An aar does not contain any dependency information by itself.
All the information of this aar is stored in pom.xml which can be found here over google maven repo.
And this will only show you what Gradle dependencies command will do, and those are the transitive dependencies meaning the direct dependencies for this aar, Which By default, Gradle resolves them automatically.
the pom.xml for com.google.firebase:firebase-firestore:24.4.0
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-firestore</artifactId>
<version>24.4.0</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>androidx.annotation</groupId>
<artifactId>annotation</artifactId>
<version>1.1.0</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.google.android.gms</groupId>
<artifactId>play-services-base</artifactId>
<version>18.0.1</version>
<scope>compile</scope>
<type>aar</type>
</dependency>
</dependencies>
<name>firebase-firestore</name>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
</project>
This pom.xml include com.google.android.gms which has its own pom.xml
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.android.gms</groupId>
<artifactId>play-services-basement</artifactId>
<version>18.1.0</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>androidx.collection</groupId>
<artifactId>collection</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>androidx.core</groupId>
<artifactId>core</artifactId>
<version>1.2.0</version>
<scope>compile</scope>
<type>aar</type>
</dependency>
<dependency>
<groupId>androidx.fragment</groupId>
<artifactId>fragment</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
<type>aar</type>
</dependency>
</dependencies>
<name>play-services-basement</name>
<licenses>
<license>
<name>Android Software Development Kit License</name>
<url>https://developer.android.com/studio/terms.html</url>
<distribution>repo</distribution>
</license>
</licenses>
</project>
What I am trying to say, Is that unless you iterate the process and fetch the POM files of the dependencies yourself, with a custom task, All you can use is gradle dependencies command to check the transitive dependencies used by your project or module.
UPDATE:
You can easily start a new gradle project by following these simple steps.
mkdir gradleExp
cd gradleExp
gradle init # 1.basic 1.groovy random name
update the empty build.gradle with the following
plugins {
id 'java'
}
repositories {
google()
mavenCentral()
}
dependencies {
implementation "com.google.firebase:firebase-firestore:24.4.0"
}
gradle dependencies # to list all
gradle dependencies --configuration compileClasspath # reduce output to show only Compile classpath for source set 'main'
NOTE: missing either google() or mavenCentral() will show some failure in the result shown.
If it is a dependency of your project you should be able to run
gradle dependencies
and see the dependency tree for your whole project (including the subtree for this artifact)
There are more details in the answer(s) to this question:
Using Gradle to find dependency tree
I have build a maven project A inside that build.xml include all test dependencies import using another pom.
when I trying to include that maven project A to gradle project B
It gives error.
I try with [--info] first it tries correct repository url and updating jar [Cached resource is up-to-date]. But finally it tries others and throws error.
How to pass profile name form gradle build file?
helps are appreciated.
Project A -> Maven Build - pom.xml
<groupId>com.spring.test</groupId>
<artifactId>Rest</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Project</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<JUnitBOM.version.number>1.0.0</JUnitBOM.version.number>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<!-- Components -->
<dependency>
<groupId>com.test</groupId>
<artifactId>JUnitBOM</artifactId>
<version>${JUnitBOM.version.number}-${project.qualifier}
</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<profile>
<id>AUAT</id>
<properties>
<env>uat</env>
<project.qualifier>SNAPSHOT</project.qualifier>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
Project B -> gradle build - build.gradle
apply plugin: 'java'
apply plugin: 'maven'
group = "com.spring.integration"
status = 'integration'
version = '1.0.0'
sourceCompatibility = 1.7
repositories {
maven { url "http://repo1" }
maven { url "http://maven.springframework.org/release" }
maven { url "http://maven.restlet.org" } }
dependencies {
compile group:'com.spring.test', name:'Rest', version:'1.0.0-SNAPSHOT', classifier: 'AUAT'
compile group:'org.restlet.jee', name:'org.restlet', version:'2.2'
compile group:'org.restlet.jee', name:'org.restlet.ext.servlet',version:'1.1'
compile group:'org.springframework', name:'spring-web', version:'3.2.1.RELEASE'
compile group:'org.slf4j', name:'slf4j-api', version:'1.7.2'
compile group:'ch.qos.logback', name:'logback-core', version:'1.0.9'
testCompile group:'junit', name:'junit', version:'4.11'
}
Error Message
Could not resolve all dependencies for configuration ':compile'.
> Could not resolve com.spring.test:Rest:1.0.0-SNAPSHOT.
Required by:
com.spring.test:0.1.0-SNAPSHOT
> Could not resolve com.test:JUnitBOM:1.0.0-${project.qualifier}.
> java.lang.IllegalArgumentException (no error message)
> java.lang.IllegalArgumentException (no error message)
> java.lang.IllegalArgumentException (no error message)
> Could not HEAD 'http://maven.restlet.org/com/spring/test/Rest/1.0.0-SNAPSHOT/Rest-1.0.0-SNAPSHOT.pom'. Received status code 409 from server: Conflict
You can make use of Gradle maven settings plugin https://plugins.gradle.org/plugin/net.linguica.maven-settings.
With the maven settings plugin, you can pass the activeProfiles, custom maven settings file.
I have a Spring boot project which is built with Gradle.
I need to migrate it to Maven, which I thought would be easy (should be), but I've encountered an issue in the Zuul depedency, or to be exact: spring-cloud-starter-zuul.
When I run the Spring boot application with the annotation #EnableZuulProxy it produces the following error:
2017-03-30 00:02:57.521 WARN 11380 --- [ost-startStop-1] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2017-03-30 00:02:57.521 INFO 11380 --- [ost-startStop-1] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2017-03-30 00:02:57.532 INFO 11380 --- [ost-startStop-1] c.netflix.config.DynamicPropertyFactory : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration#df85e3e
2017-03-30 00:02:57.556 ERROR 11380 --- [ost-startStop-1] o.s.b.c.embedded.tomcat.TomcatStarter : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'org.springframework.cloud.netflix.zuul.ZuulConfiguration$ZuulFilterConfiguration': Unsatisfied dependency expressed through field 'filters'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleHostRoutingFilter' defined in org.springframework.cloud.netflix.zuul.ZuulProxyConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter]: Factory method 'simpleHostRoutingFilter' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/http/conn/HttpClientConnectionManager
2017-03-30 00:02:57.588 WARN 11380 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
2017-03-30 00:02:57.588 ERROR 11380 --- [ main] o.s.boot.SpringApplication : Application startup failed
The error only occurs on the Maven build, which narrow the problem to the pom.xml file vs build.gradle which does run.
After tinkering with the working build.gradle file, I've managed to produce the error when commenting out this part:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.3.RELEASE")
}
}
apply plugin: 'org.springframework.boot'
So the issue is I don't use the plugin in my maven file?
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>aaa</groupId>
<artifactId>helloZuul</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.3.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zuul -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
</dependencies>
</project>
build.gradle
group 'aaa'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.3.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
sourceCompatibility = 1.8
jar {
baseName = 'gs-rest-service'
version = '0.1.0'
}
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile "org.springframework.boot:spring-boot-starter-web:1.4.3.RELEASE"
compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zuul', version: '1.2.6.RELEASE'
}
How can I make my maven project run? Or more specifically, what is the purpose of the spring boot gradle plugin?
Any help will be greatly appreciated!
You need to add dependency management in your pom.xml like below.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
I managed to get it working after a lot of trial and error.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>aaa</groupId>
<artifactId>client</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
I have the following scenario:
I want to use a project of mine (hosted on bintray.com) in another project of mine.
I set up a maven repository, uploaded artifacts and pom files and then was able to utilize the jar file(s) uploaded to the bintray maven repo just fine, with the following build.gradle file:
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'randers.test.usageTest.UsageTest'
repositories {
maven { url 'http://dl.bintray.com/randers00/NotEnoughVocab' }
jcenter()
}
dependencies {
compile(group: 'randers.notenoughvocab.core', name: 'notenoughvocab-core', version: '0.0.1', ext: 'jar')
}
jar {
manifest {
attributes "Main-Class": mainClassName
}
}
This build file successfully equips the project with my core library and even makes sources, etc. available in the IDE (IntelliJ IDEA I use)
The problem is: The core itself uses libraries, which are not gotten by gradle.
This is the pom file that is on bintray:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>randers.notenoughvocab.core</groupId>
<artifactId>notenoughvocab-core</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<licenses>
<license>
<name>GNU General Public License, Version 3.0</name>
<url>http://www.gnu.org/licenses/gpl.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<contributors>
<contributor>
<name>Ruben Anders</name>
<email>RAnders00#users.noreply.github.com</email>
<url>https://github.com/RAnders00</url>
</contributor>
</contributors>
</project>
I looked at other projects on bintray and their pom files look similar.
Declaring the dependecy the traditional and simple way works fine:
compile 'randers.notenoughvocab.core:notenoughvocab-core:0.0.1'
It doesn't work when you specify ext: 'jar', because that is used to download a single artifact. From the user guide:
Artifact only notation
As said above, if no module descriptor file can be found, Gradle by default downloads a jar with the name of the module. But sometimes, even if the repository contains module descriptors, you want to download only the artifact jar, without the dependencies. [14] And sometimes you want to download a zip from a repository, that does not have module descriptors. Gradle provides an artifact only notation for those use cases - simply prefix the extension that you want to be downloaded with '#' sign:
Example 50.5. Artifact only notation
build.gradle
dependencies {
runtime "org.groovy:groovy:2.2.0#jar"
runtime group: 'org.groovy', name: 'groovy', version: '2.2.0', ext: 'jar'
}
While only for two days now, I am definitely sold on using gradle for all of my Java projects, and drop pom.xml from the root of all my projects.
However, I would like to remain maven-compatible, in the sense that I would like for a gradle task to be able to generate a suitable pom.xml at the root of the project should the user want it.
At this moment, the only reference to a pom.xml I have is in this section of the build.gradle file (this is, with very few modifications, what is found here):
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment {
MavenDeployment deployment -> signing.signPom(deployment);
}
repository(url: sonatypeRepoURI) {
authentication(userName: sonatypeUsername,
password: sonatypePassword);
}
pom.project {
name "${name}";
packaging "bundle";
description "${description}";
url "${projectURL}";
scm {
url "${gitroscm}";
connection "${gitroscm}";
developerConnection "${gitrwscm}";
}
licenses {
license {
name "Lesser General Public License, version 3 or greater";
url "http://www.gnu.org/licenses/lgpl.html";
distribution "repo";
}
}
developers {
developer {
id "whocares";
name "whocares";
email "whocares";
}
}
}
}
}
}
How would I extract the pom.project out of this very deeply nested construct into a task which could generate a pom.xml (by default, the generated pom.xml is in build/poms/pom-default.xml and looks quite good)?
More importantly, is it possible to extract that pom.project out of uploadArchives while still being able to refer to it?
Full link to the build.gradle file: here.
You can use the gradle maven plugin. This adds the pom convention method to your project, which you can use in a task to generate a pom.xml file, like
task writeNewPom {
doLast {
pom {
project {
groupId 'org.example'
artifactId 'test'
version '1.0.0'
inceptionYear '2008'
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
}
}.writeTo("pom.xml")
}
}
Then you call gradle createPom to generate the pom.xml in the project root. Of all the things in the pom definition, you should really provide groupId, artifactId and version, other thins like licenses are not that important.
You can also look at this example for a project definition with some dependencies, and try running it to see what it produces.
Some of the new keywords were added and some techniques were deprecated. Please check
Here is my build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'org.springframework:spring-core:4.0.5.RELEASE'
compile 'org.springframework:spring-webmvc:4.0.5.RELEASE'
compile 'org.slf4j:slf4j-api:1.7.5'
runtime 'org.slf4j:slf4j-log4j12:1.7.5'
testCompile 'org.springframework:spring-test:4.0.5.RELEASE'
testCompile 'junit:junit:4.11'
testCompile "org.mockito:mockito-core:1.9.5"
testCompile "org.hamcrest:hamcrest-library:1.3"
testCompile 'javax.servlet:javax.servlet-api:3.0.1'
}
test {
testLogging {
// Show that tests are run in the command-line output
events 'started', 'passed'
}
}
task wrapper(type: Wrapper) { gradleVersion = '1.12' }
task createPom {
pom {
project {
groupId 'sg.test.spring.web.guide'
artifactId 'sg-web-initial'
version '1.0.0-SNAPSHOT'
inceptionYear '2008'
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
}
}.writeTo("pom.xml")
}
You can name the task createPom to anyTaskName as you like.
Then just run gradle clean or grale build or simply gradle createPom.
This will generate it as pom.xml in the root of the project. Although you can replace writeTo("pom.xml") with writeTo("<anyDir>/newpom.xml").
The resulting pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>sg.test.spring.web.guide</groupId>
<artifactId>sg-web-initial</artifactId>
<version>1.0.0-SNAPSHOT</version>
<inceptionYear>2008</inceptionYear>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.5.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Fast forward a few years and a few major Gradle releases, in Gradle 7.5.1 this is how one would approach this with the built-in publishing plugin (latest docs). This has the benefit over hand-writing the pom like above that it automatically gets a lot of the properties, dependencies from the normal Gradle setup:
plugins {
id("maven-publish")
}
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
tasks.named("generatePomFileForMavenPublication").configure {
def publication = publishing.publications.maven
destination = rootProject.file("${publication.artifactId}-${publication.version}.pom")
}
alternatively without mutating the built-in task we can copy it wherever needed:
tasks.register("copyPomToRoot") {
def publication = publishing.publications.maven
def generatePom = tasks.named("generatePomFileFor${publication.name.capitalize()}Publication")
dependsOn(generatePom)
def output = rootProject.file("${publication.artifactId}-${publication.version}.pom")
outputs.file(output)
doLast { output.bytes = generatePom.get().destination.bytes }
}
As a side-note: if you need a pom.xml, you probably don't just want it in your repo root, but want to publish to a proper maven repository: https://stackoverflow.com/a/70758552/253468.