Put Maven Local Project as Dependency on Gradle Project

Asked

Viewed 1,024 times

2

I have this Maven project that has all the application domain classes and logic. Then I created an external Gradle test project where I do system testing with Selenium Webdriver.

In order not to have to add all the domain classes in my Gradle project, I would like to add Maven to the dependencies, but when running the test project, it launches several errors in all the classes I use the domains.

The main issue is: how to add the Maven project as dependency on the Gradle project?

I’ve tried to put

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

in the build.Radle and

dependencies {

    compile "br:meuProjetomaven"

but it gives me this exit:

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

Could not find br:meuProjetomaven:. Searched in the following Locations: file:/C:/Users/pedro/. m2/Repository/br/meuProjetomaven//meProjetomaven-.pom file:/C:/Users/pedro/. m2/Repository/br/meuProjetomaven//meProjetomaven-.jar file:/C:/Users/pedro/git/projeto/br/meuProjetomaven//meuProjetomaven-.pom file:/C:/Users/pedro/git/projeto/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']
}
}
  • You can edit the question and post the pom.xml and the build.gradle complete?

  • @Victorstafusa made

  • Is there a reason you use Maven and Gradle instead of just Gradle?

  • @Victorstafuses the Maven project is a project the part the team is developing. Gradle is because the base framework I use to develop the tests is done in Gradle.

  • What is br.automacao-gradle and br:automacao-core?

  • the dependencies of the test automation framework

Show 1 more comment

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 to exchange ISO-8859-1 for UTF-8. The ISO-8859-1 format is hideous. But this is already the subject of another question.

Finally, this excerpt tells you what 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 try to use in Gradle br:myMavenProject it’s never going to work because meuProjetomaven and myMavenProject are very different names.

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

The third problem is that you are generating a file war, and not a file jar. A file war It wasn’t meant to be used as a dependency for anything, it was meant to be deployed on an application server and nothing more than that. This way, there is no way to use it as dependency on Gradle.

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

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

On the other hand, if what you want is to access the domain classes to do unit testing, you should add the dependency Jars (not Wars or Ears) to the 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’t or don’t 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 state how testCompile this JAR and Selenium would access WAR content only via HTTP.

  • Never use snapshots on Maven? I was curious... can I ask about it in an acceptable way?

  • @Jeffersonquesado I’ve fucked up a lot with snapshot and the day I decided to ban them for good, everything just got easier. I’ve also seen other people contraindicating snapshots. However, just by reporting experiences and saying that "I’ve heard about it," I couldn’t yet come up with a good answer to that if you opened a new question.

  • 'Cause I haven’t been able to come up with a question that isn’t broad or based on opinions, so you’ll have a little time to think about the answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.