I have a multi-project setup in maven and trying to switch to gradle. I am trying to figure out how to have one project's test dependencies include another project's test jar. Right now i have the following in ProjectA:
packageTests = task packageTests(type: Jar) {
classifier = 'tests'
from sourceSets.test.output
}
tasks.getByPath(":ProjectA:jar").dependsOn(packageTests)
And in ProjectB i have:
testCompile project(path: ':ProjectA', classifier: 'tests')
I see that my tests are failing to compile. Looks like they are missing classes defined in the test jar. When I check the build dir, i see that the ProjectA-0.1.56-SNAPSHOT-tests.jar is present.
In maven I had the following for ProjectA:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
And this for ProjectB:
<!-- Testing -->
<dependency>
<groupId>com.example</groupId>
<artifactId>ProjectA</artifactId>
<version>0.1.56-SNAPSHOT</version>
<type>test-jar</type>
</dependency>
How can I get this to work just like maven?
What you will end up with is something like
tasks.create( [
name: 'testJar',
type: Jar,
group: 'build',
description: 'Assembles a jar archive containing the test classes.',
dependsOn: tasks.testClasses
] ) {
manifest = tasks.jar.manifest
classifier = 'tests'
from sourceSets.test.output
}
// for test dependencies between modules
// usage: testCompile project(path: ':module', configuration: 'testFixtures')
configurations { testFixtures { extendsFrom testRuntime } }
artifacts {
archives testJar
testFixtures testJar
}
tasks.uploadArchives.dependsOn testJar
Related
My project has 3 submodules and it's project structure like the image below.
I am going to build moyamo-api-server [moyamo] by both maven and gradle.
moyamo has moyamo-event-domain and moyamo-common as dependencies, and moyamo-event-domain is one of submodules of moyamo-event-module. After I started moyamo-api-server in intellij as a maven project, though it has also gradle setting, I imported moyamo-common by importing its pom.xml (moyamo-common also has gradle), and imported moyamo-event-domain by importing build.gradle (moyamo-event-module and moyamo-event-domain don't have pom.xml). Importing meant here that "File/New/Module from Existing Source".
Basically when I reload maven in pom of moyamo-api-server, I cannot resolve net.infobank.moyamo:moyamo-event-domain:0.0.1-SNAPSHOT. After comment dependency of moyamo-event-domain in pom.xml of moyamo-api-server, pom sync works.
But when I run "mvn clean install" on terminal of moyamo-api-server, I get the error [ERROR] Failed to execute goal on project moyamo: Could not resolve dependencies for project net.infobank:moyamo:pom:0.0.1-SNAPSHOT: Could not find artifact net.infobank:moyamo-common:jar:0.0.1-SNAPSHOT -> [Help 1], and warning [WARNING] The POM for net.infobank:moyamo-common:jar:0.0.1-SNAPSHOT is missing, no dependency information available
I checked that moyamo has moyamo-common as a dependency at project structure.
It's the description so far for maven build.
When I tried to build by gradle, encountered this error.
I share POM of moyamo-api-server and 3 gradles.
moyamo-api-server / 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>net.infobank</groupId>
<artifactId>moyamo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>moyamo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
<org.projectlombok.version>1.18.12</org.projectlombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
...
<dependency>
<groupId>net.infobank</groupId>
<artifactId>moyamo-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- springfox-swager-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>net.infobank.moyamo</groupId>-->
<!-- <artifactId>moyamo-event-domain</artifactId>-->
<!-- <version>1.0-SNAPSHOT</version>-->
<!-- <systemPath>${basedir}/../moyamo-event-module/moyamo-event-domain/build/libs/moyamo-event-domain-1.0-SNAPSHOT.jar</systemPath>-->
<!--<!– <systemPath>${basedir}/target/lib/moyamo-event-domain-1.0-SNAPSHOT.jar</systemPath>–>-->
<!-- </dependency>-->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<!-- moyamo-event-domain dependency ./lib 에 추가.-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>system</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<configuration>
<rules>
<dependencyConvergence/>
</rules>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.8</version>
<executions>
<execution>
<id>generate-docs</id>
<phase>prepare-package</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
<configuration>
<backend>html</backend>
<doctype>book</doctype>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-asciidoctor</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.outputDirectory}/static/docs
</outputDirectory>
<resources>
<resource>
<directory>
${project.build.directory}/generated-docs
</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!--rest docs end -->
</plugins>
</build>
</project>
moyamo-api-server / build.gradle
jar { enabled = false }
bootJar { enabled = true }
bootRun {enabled = true}
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}
sourceCompatibility = '1.8'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-amqp'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j:1.0.2.RELEASE'
implementation 'org.springframework.amqp:spring-rabbit-test'
testImplementation 'org.springframework.amqp:spring-rabbit-test'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
developmentOnly "org.springframework.boot:spring-boot-devtools"
testImplementation 'org.springframework.security:spring-security-test'
implementation 'org.hibernate:hibernate-spatial'
implementation 'org.hibernate:hibernate-ehcache'
implementation 'org.hibernate:hibernate-search-orm'
implementation 'org.hibernate:hibernate-search-elasticsearch'
implementation 'org.apache.commons:commons-lang3:3.11'
implementation 'com.auth0:java-jwt:3.10.2'
implementation 'commons-io:commons-io:2.6'
implementation 'io.findify:s3mock_2.12:0.2.5'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:2.2.2.RELEASE'
runtimeOnly 'mysql:mysql-connector-java:8.0.19'
testImplementation 'junit:junit:4.12'
implementation 'com.sksamuel.scrimage:scrimage-core:4.0.5'
compileOnly 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
implementation 'com.google.code.gson:gson:2.8.6'
}
moyamo-common / build.gradle
bootJar { enabled = false }
jar { enabled = true }
/*
* This file was generated by the Gradle 'init' task.
*/
ext {
set('springCloudVersion', "2020.0.1")
set('mapstructVersion', "1.3.1.Final")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.hibernate:hibernate-search-orm'
implementation 'org.hibernate:hibernate-ehcache'
implementation 'org.hibernate:hibernate-search-elasticsearch'
implementation 'org.hibernate:hibernate-spatial'
implementation 'com.bedatadriven:jackson-datatype-jts:2.2'
implementation "org.mapstruct:mapstruct"
annotationProcessor "org.mapstruct:mapstruct-processor"
testAnnotationProcessor "org.mapstruct:mapstruct-processor:"
implementation 'io.github.resilience4j:resilience4j-micrometer:1.1.0'
implementation 'io.github.resilience4j:resilience4j-metrics:1.1.0'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:2.2.2.RELEASE'
implementation 'io.micrometer:micrometer-registry-prometheus:1.2.0'
implementation 'com.auth0:java-jwt:3.10.2'
implementation 'commons-io:commons-io:2.6'
implementation 'io.findify:s3mock_2.12:0.2.5'
implementation 'org.springframework.cloud:spring-cloud-aws-context:2.2.6.RELEASE'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.apache.shiro:shiro-all:1.2.3'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.11.0'
implementation 'org.apache.commons:commons-lang3:3.11'
implementation 'com.sksamuel.scrimage:scrimage-core:4.0.5'
implementation 'org.apache.poi:poi:4.1.2'
runtimeOnly 'mysql:mysql-connector-java:8.0.19'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.amqp:spring-rabbit-test'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'junit:junit:4.12'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
moyamo-event-module / build.gradle
bootJar { enabled = false }
jar { enabled = true }
/*
* This file was generated by the Gradle 'init' task.
*/
ext {
set('springCloudVersion', "2020.0.1")
set('mapstructVersion', "1.3.1.Final")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.hibernate:hibernate-search-orm'
implementation 'org.hibernate:hibernate-ehcache'
implementation 'org.hibernate:hibernate-search-elasticsearch'
implementation 'org.hibernate:hibernate-spatial'
implementation 'com.bedatadriven:jackson-datatype-jts:2.2'
implementation "org.mapstruct:mapstruct"
annotationProcessor "org.mapstruct:mapstruct-processor"
testAnnotationProcessor "org.mapstruct:mapstruct-processor:"
implementation 'io.github.resilience4j:resilience4j-micrometer:1.1.0'
implementation 'io.github.resilience4j:resilience4j-metrics:1.1.0'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:2.2.2.RELEASE'
implementation 'io.micrometer:micrometer-registry-prometheus:1.2.0'
implementation 'com.auth0:java-jwt:3.10.2'
implementation 'commons-io:commons-io:2.6'
implementation 'io.findify:s3mock_2.12:0.2.5'
implementation 'org.springframework.cloud:spring-cloud-aws-context:2.2.6.RELEASE'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.apache.shiro:shiro-all:1.2.3'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.11.0'
implementation 'org.apache.commons:commons-lang3:3.11'
implementation 'com.sksamuel.scrimage:scrimage-core:4.0.5'
implementation 'org.apache.poi:poi:4.1.2'
runtimeOnly 'mysql:mysql-connector-java:8.0.19'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.amqp:spring-rabbit-test'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'junit:junit:4.12'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
rootProject.name = 'moyamo-event-module'
include 'moyamo-event-domain'
include 'moyamo-event-server'
moyamo-event-module / moyamo-event-domain / build.gradle
plugins {
id 'java'
}
jar { enabled = true }
//bootJar { enabled = false}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
compileOnly 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10'
}
test {
useJUnitPlatform()
}
I hope to have moyamo-api-server built by both Maven and Gradle.
The weird thing is that in moyamo-parent (root project), which has moyamo-api-server as a dependency, I can build entire project successfully by run "./gradlew clean build -x test" and get each project built by jar or war.
moyamo-parent has only gradle.
But moyamo-api-server cannot be built indenpendently.
So I add moyamo-parent gradle here for clear understanding my trouble.
moyamo-parent / build.grale & settings.gradle
buildscript {
ext {
springBootVersion = '2.4.7'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'io.spring.dependency-management'
allprojects {
group = 'net.infobank.moyamo'
version = '0.0.1-SNAPSHOT'
}
repositories {
mavenCentral()
}
subprojects {
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "2020.0.3")
set('mapstructVersion', "1.3.1.Final")
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-amqp'
implementation 'org.springframework.boot:spring-boot-starter-validation'
runtime("org.springframework.boot:spring-boot-properties-migrator")
compile("org.mapstruct:mapstruct:${mapstructVersion}")
compileOnly("org.mapstruct:mapstruct-processor:${mapstructVersion}")
annotationProcessor("org.mapstruct:mapstruct-processor:${mapstructVersion}")
implementation group: 'com.drewnoakes', name: 'metadata-extractor', version: '2.16.0'
compileOnly 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
implementation 'org.hibernate:hibernate-spatial:5.2.14.Final'
implementation 'org.hibernate:hibernate-ehcache:5.4.13.Final'
implementation 'org.hibernate:hibernate-search-orm:5.11.9.Final'
implementation 'org.hibernate:hibernate-search-elasticsearch:5.11.9.Final'
implementation 'org.elasticsearch.client:elasticsearch-rest-client:6.8.12'
implementation 'org.elasticsearch.client:elasticsearch-rest-client-sniffer:5.6.16'
}
task initSourceFolders {
sourceSets*.java.srcDirs*.each {
if (!it.exists()) {
it.mkdirs()
}
}
sourceSets*.resources.srcDirs*.each {
if (!it.exists()) {
it.mkdirs()
}
}
}
}
project(':moyamo') {
dependencies {
compile project(':moyamo-common')
compile project(':moyamo-event-domain')
implementation 'org.springframework.boot:spring-boot-starter-amqp'
}
}
project(':moyamo-admin') {
dependencies {
compile project(':moyamo-common')
implementation 'org.springframework.boot:spring-boot-starter-amqp'
}
}
project(':moyamo-scheduler') {
dependencies {
compile project(':moyamo-common')
compile project(':moyamo-event-domain')
implementation 'org.springframework.boot:spring-boot-starter-amqp'
}
}
//def common = project(':moyamo-common')
//def domain = project(':moyamo-event-domain')
/*configure ([common]) {
apply plugin: "io.spring.dependency-management"
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
}
}*/
rootProject.name = 'moyamo-parent'
include ':moyamo', //moyamo-api-server
':moyamo-event-domain',
':moyamo-common',
':moyamo-admin',
':moyamo-scheduler'
for (project in rootProject.children) {
project.projectDir = file("../moyamo-event-module/${project.name}")
}
project(':moyamo').setProjectDir(new File(settingsDir, '../moyamo-api-server'))
project(':moyamo-common').setProjectDir(new File(settingsDir, '../moyamo-common'))
project(':moyamo-admin').setProjectDir(new File(settingsDir, '../moyamo-admin'))
project(':moyamo-scheduler').setProjectDir(new File(settingsDir, '../moyamo-scheduler'))
I need your helps, thanks in advance.
I am trying to convert an old maven to gradle. Below is the maven block I am trying to convert to gradle equivalent.
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.2</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<generateDirectory>${project.basedir}/target/generated-sources/twg</generateDirectory>
<schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory>
<schemaIncludes>
<include>TWG.wsdl</include>
</schemaIncludes>
</configuration>
</plugin>
</plugins>
I tried something like this below
task generateJavaClasses {
System.setProperty('javax.xml.accessExternalSchema', 'all')
def jaxbTargetDir = file("src/main/java/")
doLast {
jaxbTargetDir.mkdirs()
ant.taskdef(
name: 'xjc',
classname: 'com.sun.tools.xjc.XJCTask',
classpath: configurations.jaxb.asPath
)
ant.jaxbTargetDir = jaxbTargetDir
ant.xjc(
destdir: '${jaxbTargetDir}',
package: 'newjavaFromWsdl',
schema: 'src/main/resources/TWG.wsdl',
language: 'WSDL'
)
}
}
added the jaxb dependency like
dependencies {
implementation 'com.sun.xml.bind:jaxb-xjc:4.0.0'
}
but not working actually, what am i doing wrong here
There was no issue with the script, but in Gradle, the dependency i used was wrong.
it should be like
configurations {
jaxb
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web:2.7.1'
jaxb(
'com.sun.xml.bind:jaxb-core:3.0.2',
'com.sun.xml.bind:jaxb-xjc:2.3.6',
'com.sun.xml.bind:jaxb-impl:2.3.6'
)
}
I have a project with cucumber and maven also I am using the JUnit.
I am able to run and build my project successfully from Eclipse.
Now I want to run the test from command line in another system which does(should) not have eclipse or cucumber installed. I have an idea that we can create a JAR from jar we can run the tests by java cli commands.
Below are the combinations I am trying to run my tests from , also I am pasting the pom.xml and RunCukesTest.java file..
pom.xml
<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>pmc</groupId>
<artifactId>se</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>storeEnabler</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.52.0</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11-beta3</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
<build>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/java</directory>
</testResource>
<testResource>
<directory>${project.basedir}/src/test/resource</directory>
</testResource>
</testResources>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<fork>true</fork>
<executable>C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe</executable>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.18</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>cucumber.api.cli.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
RunCukesTest.java
package se.stepDefinations;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(features = "src/test/resource/features/Printing.feature:117", plugin = { "pretty",
"html:target/cucumber-html-report" }, glue = { "se.stepDefinations" }, tags = {})
public class RunCukesTest {
public static void main(String[] args) throws Exception {
}
}
I have added JUNIT Jar in Class path.
I am generating jars in 2 ways,
1) Exporting JAR using - > Project ->Export -> JAR File
Selecting MAIN Class in the last step as : RunCukesTest as I have defined main method here for the entry point(do we require this main method in this class???)
After the export , I am running below command ,
1.1 java -jar xyz.jar
I get error : NoClassDef found : org/junit/runner/JUnitCore
So I ran it this way :
1.2 java -cp xyz.jar;junit-4.12.jar org.junit.runner.JUnitCore
It said,
JUnit version 4.12
Time:0.001
OK(0 tests)
It still didnt work , So I appended the RunCukesTest file namespace at the last,
1.3 java -cp xyz.jar;junit-4.12.jar org.junit.runner.JUnitCore se.stepDefinations.RunCukesTest
It gave me error :Type cucumber.api.junit.Cucumber not present
2) So I gave up on the option for Export of jar and I am trying now to use the JAR from the maven Build.
I selected the POM to run with Maven Build and it created 2 jars in the target folder ,
1 with name xyz-0.0.1-SNAPSHOT with 16kb another with 2nd with
xyz-0.0.1-SNAPSHOT-jar-with-dependencies with 33mb
1) I ran the bigger file with dependencies using
java -jar xyz-0.0.1-SNAPSHOT-jar-with-dependencies.jar
It gave me message :
Got no path to feature directory
2) So I tried appending the namespace to RunCukesTest file,
java -jar xyz-0.0.1-SNAPSHOT-jar-with-dependencies.jar se.stepDefinations.RunCukesTest
I received an error : not a file or directory found
, as of course as the error says , it is trying to find a feature inside the target folder.
Again , I want to run the JAR independent of any such project file dependencies in any other computer like an executable.
Any help would be appreciated. Thanks.
I would divide the problem you are thinking of in two parts.
Create an executable jar
Run Cucumber from your own main method
Creating an executable jar using Maven can be done in different ways. One way of doing it is described here:
http://www.thinkcode.se/blog/2011/03/05/create-an-executable-jar-from-maven
It is a small example that only focuses on executing something from a command line like this:
java -jar executable-example.jar
The example contains all dependencies. They are all bundled in the same jar. No need for any additional jars.
Next step would be to execute Cucumber from a main method. My approach would be to write a main that executes the Cucumber main method used for the command line version of Cucumber. The main method used to run cucumber from a command line lives in the cucumber-java library. You will find it at cucumber.api.cli.Main
Running a main method from another main method is done like this:
public static void main(String[] args) throws Throwable {
String[] arguments = {"foo", "bar"};
cucumber.api.cli.Main.main(arguments);
}
where arguments are the command line arguments you always want to execute Cucumber with.
Given these two steps, you should be able to execute Cucumber from your own executable jar wherever you are able to execute a jar at all.
Notice that you are mixing library version for Cucumber in your pom. I would use the latest version of all libraries. Compare cucumber-java, cucumber-testng and cucumber-junit. The latest Cucumber version is 1.2.4. I would use it for all of them.
More information about running Cucumber from a command line can be found here: https://cucumber.io/docs/cucumber/api/#from-the-command-line
I'd like to extend the accepted answer to support Gradle, since this might be helpful for someone.
Given this is your project's structure
.root
src/
main/
java/ --> Put your .java files here
CukesRunner.java --> This is your main file
resources
features/ --> Put your .feature files here
The build.gradle file should look like something like this:
apply plugin: 'java'
sourceSets {
main {
java {
srcDirs = ['src/main/java']
}
resources {
srcDirs = ['src/main/resources']
}
}
}
dependencies {
// Include your dependencies here
compile '...'
}
configurations {
cucumberRuntime {
extendsFrom compile
}
}
jar {
from {
// Package all dependencies in the .jar
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
manifest {
// Indicate the main class for the .jar file
attributes 'Main-Class': 'CukesRunner'
}
}
Then you can customize your CukesRunner.java just like the accepted answer suggested, but making sure to call for the features you compressed together with your jar file:
public class CukesRunner {
public static void main(String[] args) throws Throwable {
final String[] arguments = new String[]{
"--glue", "<my Java classes packages reference>",
"classpath:features" // This will look for the classpath inside the jar file
};
cucumber.api.cli.Main.main(arguments);
}
}
#SpringBootApplication
public class Application {
public static void main(final String[] args) throws Throwable {
// SpringApplication.run(TestApplication.class, args);
JUnitCore.main(CucumberTest.class.getCanonicalName());
}
}
i'm switching from maven to gradle.
Here is what i used to have in my pom.xml
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
</execution>
<execution>
<id>process-test</id>
<goals>
<goal>process-test</goal>
</goals>
<phase>generate-test-sources</phase>
<configuration>
<sourceDirectory>./test</sourceDirectory>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>rockpalm.it</groupId>
<artifactId>ic2-annotation-processor</artifactId>
<version>1.2.1-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
My build.gradle looks like :
plugins {
id "net.ltgt.apt" version "0.15"
id 'net.ltgt.apt-eclipse' version '0.15'
}
dependencies {
annotationProcessor "rockpalm.it:ic2-annotation-processor:1.2.1-SNAPSHOT"
}
ext {
eclipseAptFolder = '.apt_generated'
eclipseSettingsDir = file('.settings')
}
eclipse {
jdt.file.withProperties {
it['org.eclipse.jdt.core.compiler.processAnnotations'] = 'enabled'
}
}
tasks.eclipseJdt {
doFirst {
def aptPrefs = file("${eclipseSettingsDir}/org.eclipse.jdt.apt.core.prefs")
aptPrefs.parentFile.mkdirs()
aptPrefs.text = """\
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=true
org.eclipse.jdt.apt.genSrcDir=${eclipseAptFolder}
org.eclipse.jdt.apt.reconcileEnabled=true
""".stripIndent()
file('.factorypath').withWriter {
new groovy.xml.MarkupBuilder(it).'factorypath' {
project.configurations.annotationProcessor.each { dep->
factorypathentry(
kind:'EXTJAR',
id:dep.absolutePath,
enabled:true,
runInBatchMode:false
)
}
}
}
}
}
But when I use Gradle > Refresh gradle project it does not configure the .factorypath of eclipse with my annotation processor, it enables it but without setting the actual processor on the processor list.
When I run the gradle build I can actually see my generated code in build/generated/source/apt/main/...my packages/classes but since it's not enabled in eclipse I have nothing in .apt_generated folder.
EDIT
I got gradle to build the factorypath correctly with the tasks.eclipseJdt part of the build.gradle but eclipse doesn't seem to build anything in the .apt_generated still. How can I debug eclipse gradle build to see what's happening ?
Any help appreciated, Thanks
You usually don't need the eclipse configuration closure from the plugin example, just use:
plugins {
id 'net.ltgt.apt' version '0.15'
id 'net.ltgt.apt-eclipse' version '0.15'
}
dependencies {
annotationProcessor 'rockpalm.it:ic2-annotation-processor:1.2.1-SNAPSHOT'
}
Execute gradle eclipse to setup the factory path and refresh the project in Eclipse.
I'm having multi-project Gradle config:
-- root (folder 'gradle_test')
L--wrapper (depends on some 3rd-party maven libs)
L--module1 (depends on wrapper)
L--app
I need module1 jar (and wrapper jar as transitive dependency) to be published in local maven repo.
root build.gradle:
// for maven
ext {
groupId = 'mygroup'
version = '3.0'
}
wrapper build.gradle:
apply plugin: 'maven'
...
// maven pom
install {
repositories.mavenInstaller {
pom.groupId = rootProject.ext.groupId
pom.artifactId = 'wrapper'
pom.version = rootProject.ext.version
}
}
module1 build.gradle:
dependencies {
compile project(':wrapper')
...
}
// maven pom
install {
repositories.mavenInstaller {
pom.groupId = rootProject.ext.groupId
pom.artifactId = 'module1'
pom.version = rootProject.ext.version
}
}
Howeven when installing module1 into local maven cache i can see dependency to 'wrapper' module is generated incorrectly (version is not specified). module1 pom.xml in repo:
<?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>mygroup</groupId>
<artifactId>module1</artifactId>
<version>3.0</version>
<dependencies>
<dependency>
<groupId>gradle_test</groupId> // error 1: gradle project name (instead of overriden mvn groupId)
<artifactId>wrapper</artifactId>
<version>unspecified</version> // error 2: not set at all
<scope>compile</scope>
</dependency>
</dependencies>
</project>
In other words Gradle does not use maven module groupId/artifactId/version for maven dependencies mapped from compile project(:wrapper) declaration.
How can i do/fix it?
I had to write for wrapper and module1:
// mvn
group = rootProject.ext.groupId
version = rootProject.ext.version
and you can remove:
install {
repositories.mavenInstaller {
...
}
}
since Gradle will use project.group as mvn module groupId and project.version as mvn module version:
https://docs.gradle.org/current/userguide/maven_plugin.html#sec:maven_pom_generation