Put local Maven project as dependency on Gradle project

2

I have this Maven project that has all application domain classes and logic. From there I created an external Gradle test project where I do system tests with Selenium WebDriver.

To not need to add all the domain classes in my gradle project, I would like to add Maven to dependencies, but at the time of running the test project, it throws several errors in all classes that use the domains.

The main question is: how do I add the maven project as dependency in the gradle project?

I've tried to put

repositories {
    mavenLocal()
    maven { url "../meuProjetoMaven" }

in build.grad and

dependencies {

    compile "br:meuProjetomaven"

but it gives me this output:

  

Could not resolve all files for configuration ': testCompileClasspath'.

     

Could not find br: myProjetomaven :.     Searched in the following locations:         file: / C: /Users/pedro/.m2/repository/br/meuProjetomaven//meuProjetomaven-.pom         file: / C: /Users/pedro/.m2/repository/br/meuProjetomaven//meuProjetomaven-.jar         file: / C: /Users/pedro/git/project/br/meuProjetomaven//meuProjetomaven-.pom         file: / C: /Users/pedro/git/project/br/meuProjetomaven//meuProjetomaven-.jar

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>br</groupId>
<artifactId>meuProjetomaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>meuProjetomaven</name>
<description>Descricao</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>ISO-8859-1</project.build.sourceEncoding>
    <project.reporting.outputEncoding>ISO-8859-1</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <dependency>
        <groupId>br</groupId>
        <artifactId>package-release-framework</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
    </dependency>

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

build.gradle

/*
Script de build dos testes para o meuProjetoMaven
**/

/********************************** Repositórios *****************************************/

buildscript {
    repositories {
    mavenLocal()
    maven { url "http://SomeRepo.com/artifactory/libs-release" }
    maven { url "http://SomeRepo.com/artifactory/libs-snapshot" }

}
dependencies {
    classpath 'br:automacao-gradle:0.1.1'
}
}

repositories {
mavenLocal()

maven { url "http://SomeRepo.com/artifactory/libs-release" }
maven { url "http://SomeRepo.com/artifactory/libs-snapshot" }

}

/*********************************** Build ***********************************************/

apply plugin: 'java'
apply plugin: 'br.automacao-gradle'

dependencies {
    testCompile "br:automacao-core:0.1.0"
    //compile "br:myMavenProject" << esta linha está comentada pois foi de uma tentativa que não deu certo de utilizar
}

sourceSets{
    main{
    java.srcDirs = []
}
test{
    java.srcDirs = ['src', 'testes']
    resources.srcDirs = ['test-resources']
}
}
    
asked by anonymous 13.06.2018 / 21:08

1 answer

1

Your maven file has some problems. First, remove these things from it:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/>
    </parent>
        <dependency>
            <groupId>br</groupId>
            <artifactId>package-release-framework</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

I also suggest replacing ISO-8859-1 with UTF-8. The ISO-8859-1 format is awful. But this is already the subject of another question.

Finally, this snippet says which version you are generating:

    <groupId>br</groupId>
    <artifactId>meuProjetomaven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

Here are three problems. The first is that this says that the name of your project is br:meuProjetomaven:0.0.1-SNAPSHOT and therefore trying to use it in gradle br:myMavenProject will never work, because meuProjetomaven and myMavenProject are very different names.

The second is that SNAPSHOT only serves for headache and creates much more problems than it solves. I recommend not using SNAPSHOT never. In my opinion, this SNAPSHOT business should never have been invented.

The third problem is that you are generating a war file, not a jar file. A war file was not meant to be used as a dependency for anything, it was meant to be deployed to an application server, and nothing more. This way, there is no way to use it as a dependency in the gradle.

Using compile or testCompile is to put the JAR in the classpath. Only JAR, not WAR or EAR. WAR and EAR files should never be in the classpath of anything.

If you just want to test your application in Selenium, then your test suite should not have access to your domain classes and should only access your application via HTTP. you mount a script somewhere that deploys your application, uploads it, uses the gradle to run the test, and downloads the application. Or, within the test itself you do this process. There is a way to upload the application and deploy within the gradle, but it depends a lot on the type of server you have and it would already be topic for another question.

On the other hand, if you want to access domain classes for unit testing, you should add dependency JARs (and not WARs or EARs) in compile or testCompile .

If you want a hybrid approach, where you test the application via Selenium and have access to the domain classes, I recommend separating the two. If you can not or do not want to separate, the solution would be to split your WAR into two: A JAR file with the domain classes and a WAR file that depends on the JAR and leaves it in a format that can be deployed as an application. Your test would declare as testCompile this JAR and Selenium would access WAR content only via HTTP.

    
13.06.2018 / 23:24