Spring Cloud Contract test cannot find stubs from classpath in Intellij - spring-cloud

I have written a very simple contract test for HTTP with Spring Cloud Contract. I created the producer which has a contract definition under /src/test/resources/contracts:
package contracts
import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
method 'GET'
url '/documents/123456789'
headers {
contentType('application/json')
}
}
response {
status OK()
body([
id : 123456789,
status: "VALID"
])
headers {
contentType('application/json')
}
}
}
The producer is a maven module with a definition:
<groupId>com.sample</groupId>
<artifactId>rest-producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rest-producer</name>
I only deployed this producer to my maven local repository. Afterwards, I created a consumer with dependency to the producer stubs:
<dependency>
<groupId>com.sample</groupId>
<artifactId>rest-producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<classifier>stubs</classifier>
<scope>test</scope>
</dependency>
and I defined a simple test which uses the classpath to search for the stubs:
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
#AutoConfigureStubRunner(ids = "com.sample:rest-producer:+:stubs:8080")
class DocumentReaderIntTest {
...
}
When I run this test with maven build via clean install all works fine. However, when I try to run this test via RunConfiguration from Intellij then I get an exception that stubs cannot be found. What should I set up to make this test works from Intellij as well?
Edit:
What I found is when I open only the consumer project then on the classpath it has jar from local maven repo:
/Users/adam/.m2/repository/com/sample/rest-producer/0.0.1-SNAPSHOT/rest-producer-0.0.1-SNAPSHOT-stubs.jar
However, when I import the producer in the same project it uses target/classes folder in the classpath so it cannot find contract definition:
/Users/adam/projects/rest-producer/target/classes

Yup that's a known issue with Intellij. Intellij when you reference classpath dependency in a multimodule project will not build the stubs jar but will reference the other module's code directly. Maybe filing an issue to Intellij would make sense?

Related

How to load selected beans from external library in micronaut

I'm used to creating custom Spring Boot based libraries with a number of different beans per library. A target Spring Boot app then would use #Import to make a subset of those Beans available w/out importing all of the beans from an external library. An external library would also include spring integration tests (i.e. #SpringBootTest).
I'm trying to replicate this functionality in Micronaut. I've created an external library using 'io.micronaut.library' gradle plugin. It also contains some micronaut integration tests (i.e. #MicronautTest).
Per https://docs.micronaut.io/latest/guide/#beanImport I expected none of the beans to be imported by default. However, I've noticed that a class annotated with #Singleton was imported by default. I suspect this is due to its inclusion in the META-INF/services/io.micronaut.inject.BeanDefinitionReference within the library jar. If I remove META-INF/services/io.micronaut.inject.BeanDefinitionReference from the jar manually then none of the beans are imported by default and I can use explicit #Import to import what is needed.
The package of the library is com.some.micronaut.http while application package is com.some.somethingelse so the namespaces do not match. Both the library and the application are written in Kotlin.
Is there a way to control which beans are imported from an external library? Or can I control which bean references are included in META-INF/services/io.micronaut.inject.BeanDefinitionReference?
Here is a relevant build.gradle portion from the external library:
plugins {
id("org.jetbrains.kotlin.jvm") version "${kotlinVersion}"
id("org.jetbrains.kotlin.kapt") version "${kotlinVersion}"
id("org.jetbrains.kotlin.plugin.allopen") version "${kotlinVersion}"
id 'io.micronaut.library' version "3.1.1"
}
dependencies {
implementation("io.micronaut:micronaut-http-client")
implementation("io.micronaut:micronaut-jackson-databind")
//test
testImplementation 'io.micronaut.test:micronaut-test-junit5'
testImplementation 'org.junit.jupiter:junit-jupiter-params'
//wiremock for http client tests
testImplementation 'com.github.tomakehurst:wiremock-jre8:2.32.0'
}
I'm flexible about usage of io.micronaut.library plugin but I do need to be able to run #MicronautTest as part of the library build.
For now, I've removed META-INF/services/io.micronaut.inject.BeanDefinitionReference from the library jar explicitly in my build.gradle:
jar {
exclude('META-INF/services/io.micronaut.inject.BeanDefinitionReference')
}
and then used #Import in my application to explicitly import the desired components. I've also submitted an enhancement request.

Not able to compile library extend from mllib

I am working on a ml project using apache spark and maven. I create two library for the project - one called "rmml" which extends Spark mllib library by adding a new FactorizationMachine Algorithm to "org.apache.spark.mllib.regression" and the other library called "dataprocess" uses this new algorithm I added.
In Intellij on my laptop, I am able to call and run the FM algorithm fine in "dataprocess", and I am able to compile "rmml", however I hit an error when try to compile "dataprocess" library with "error: object FactorizationMachine is not a member of package "org.apache.spark.mllib.regression". I am not a java developer, so I am having a hard time figure this out. Any help would be great, thanks!
This pom of "dataprocess" library that imports "rmml"
<dependency>
<groupId>com.something</groupId>
<artifactId>rmml</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
This is pom of "rmml" project
<groupId>com.something</groupId>
<artifactId>rmml</artifactId>
<version>1.0-SNAPSHOT</version>
And here is class path and file path of "rmml" project
As Luis suggested "need to publish rmml to the local repository"

how to exclude specific jar if possible from child module itself or war

child module has below dependency so this alfresco-repository-5.1.1-tests.jar is coming that i can see in Maven dependencies in eclipse.I will use this jar only for compiling test classes.
<groupId>org.alfresco</groupId>
<artifactId>alfresco-repository</artifactId>
<version>5.1</version>
<classifier>tests</classifier>
<scope>test</scope>
I have parent module which will generate war which has this child module has dependency.so this jar is coming also in war.
so how to exclude this jar in war?
When you package a Maven project to a WAR file, you don't package any dependencies mentioned within your pom.xml inside your WAR. your WAR file will include your pom.xml and your .class files in it, without dependencies.
If you need that dependency for testing purposes only, the test scope
<scope>test</scope>
is enough. That'll include the requested dependency in your classpath only when running your JUnits or any other tests. You can verify that if you'll execute the test phase & compile phase separately in debugging mode, and make sure that in test phase it's being included in your classpath, and in compile phase, it doesn't. To debug the test phase in command-line:
mvn -X test

Access CQ5 project bundles on same instance

I have two project bundles my local CQ/AEM server. Project A contains some java Util class methods which can be utilized in project B as well.
While developing, how do I import my project A classes in project B to access the methods so that I do not have to duplicate the methods again?
I tried adding dependency in my Project B bundle pom.xml as below. Is this correct?
<dependency>
<groupId>com.project-a</groupId>
<artifactId>cq-project-a</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
I get missing artifact error for this.
"Missing artifact com.project-a:cq-project-a:jar:1.0-SNAPSHOT"
Please suggest how the import can be done.
Thanks
I guess you forgot to build project a using mvn install. The dependency will be searched in your local maven repo.
This solution may fix you issue: update your pom.xml on project a, modify groupId, artifactId, version, packaging tags and make sure they look likes:
<groupId>com.project-a</groupId>
<artifactId>cq-project-a</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
Then run mvn clean install on project a, run mvn clean install one more time one project b. I applied this to my last project, I hope it works for you.

Spring 4.0.6 release - #RequestMapping issue

I have downloaded (using Maven) Spring release 4.0.6. How ever in the Controller class I got an error.
#RequestMapping(value = "/", method = RequestMethod.GET)
The error was that #RequestMapping was not found. STC/Eclipse itself gave a suggestion to resolve it. And when I clicked on the suggestion it mentioned about copying some thing to /.m2/repository. And then the error was fixed as well.
What exactly did STC/Eclipse IDE do?
It is likely that STS/Eclipse added the spring-webmvc dependency to your pom.xml.
This triggered maven to donload that depency from maven central to your local maven repository cache (.m2 folder).
Because the spring-webmvc dependency was added to your pom.xml its the jar and its classes becomes available for your project.