Maven property overriding conflict between parent/bom - spring-boot

I am currently facing an issue with my maven configuration. I was thinking it will work in a way where versions in MyBom will have higher priority on the grand-grand-parent defined versions.
This is the setup :
enter image description here
In spring-dependencies, I have this version <atomikos.version>4.0.6</atomikos.version>.
In myBom, I have this version <atomikos.version>5.0.106</atomikos.version>.
Both spring-dependencies and MyBom have the
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jta</artifactId>
<version>${atomikos.version}</version>
</dependency>
I have imported the bom as usual in "MyParent":
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>myBom</artifactId>
<version>${myBom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Finally, in "MyProject", when I am showing the effective pom, it uses the version 4.0.6 coming from the spring-dependencies.
I was expecting the version to be 5.0.106 as the bom redefine it in a sublayer.
Note that it can work with any dependency which is common between bom and parent.
So, currently, my only viable solution is to manually set the the version in "MyParent" which is making the creation of "MyBom" useless...
Can you confirm what is correct ? My assumption (meaning I have a misconfiguration somewhere) or the current behavior, meaning "MyBom" is worthless.

As far as I know, "direct" entries in the dependencyManagement always take precedent over BOMs, not matter what the inheritance level is.
You should probably just override the <atomikos.version> property.

This is not possible to override parents version from "MyBom" project. The only solution is to remove the spring-boot-starter-parent as parent and import the spring-dependencies at same level of "MyBom" import.
I found the solution but I would like to thank #J Fabian Meyer to also point the correct solution.

The dependencyManagement is just a declaration of what the version of the dependency should be when really used as a project dependency. Thus you still should declare (use) the myBom dependency. Also, the scope "import" is invalid.
See the documentation
https://maven.apache.org/pom.html#dependency-management
https://maven.apache.org/pom.html#Dependencies

Related

springdoc-openapi doesnt pick up changes in controller classes

I have a 3-module Springboot multi-module maven project with a structure like so:
parent
|- module 1
|- module 2
|- module 3
The maven dependency for spring-openapi is in the parent pom, while the main class is in module 1.
The problem is that when I examine the Swagger api, I only see the documentation for the rest endpoints from module 1, and not those in 2 or 3.
I experimented by putting the openapi dependency in module 3 and re-building but that doesn't make a difference. Is it not possible to get all the rest-endpoints across all modules published all at once?
Some more info: here are some properties from my application.properties:
springdoc.packagesToScan=com.sxxxxxxxxx
springdoc.pathsToMatch=/a/**, /d/**
These are my dependencies:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.9</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-security</artifactId>
<version>1.6.9</version>
</dependency>
I thought that this Q&A might help (but it didnt): OpenAPI & spring-doc not finding all mappings in a controller class
I've been doing some experimentation:
I added a Test Controller, and it appeared in the docs. Then I deleted the Test Controller from the code base, but it still appears in the docs
I've changed the http response codes to some of the endpoints in the documentation, but those aren't picked up.
It almost seems like once it does the initial generation of documentation for a rest controller, it forgets about looking at it again for any further changes.
Does any one have an idea?
Thanks

How to configure Swagger with Openapi

I just started using springdoc-openapi. I want to be able to customize the Swagger for things like background color, logo, etc. Is there a way to do that? I see that springdoc-openapi-ui includes webjars/swagger-ui, but I'd hate to just run a customize version. Would prefer to do it as an update so it doesn't interfere with future upgrades
Just to experiment, I've tried copying the entire swagger-ui distribution to my resources directory: resources/swagger-ui. I've also tried resources/webjars/swagger-ui.
In my pom, I have
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.6</version>
<exclusions>
<exclusion>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
</exclusion>
</exclusions>
</dependency>
So it should only use my local version. But I get a 404
GET "/swagger-ui/index.html", parameters={}
Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/webjars/]]
Resource not found
Completed 404 NOT_FOUND
Not sure why it's not finding it at swagger-ui/index.html
It looks like you need to set some of the variables in the .properties file:
try
springdoc.swagger-ui.path=/swagger <- sets the url path so you can see localhost:8080/swagger/index.html
springdoc.swagger-ui.enabled=true <- sets the swagger-ui to enabled
springdoc.api-docs.enabled=true <- sets the springdoc openapi enpoint to enabled
I would look here for other property setting you might need - such as turning off swagger in production, etc...
https://springdoc.org/#properties
As for customization, you will have to create your own .html page and disable the use of the default one.
springdoc.swagger-ui.path=MyNewHtmlFile.html
I think is the property name for setting your own .html file.

Spring Data Projection not working

I want to use spring projection in my project. I am doing exactly as mentioned at this url http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections
interface NoAddresses {
String getFirstName();
String getLastName();
}
The only difference is my interface is public. and is not in the same package as the repository. Is that a requirement?
Still I see the whole entities are being returned, instead of just the projected columns.
I have
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.2.RELEASE</version>
</dependency>
Doesn't work. Do i need to add some dependency to make the projection work? I am not using spring-boot but just the spring-data stuff in my project, running on tomcat server.
thanks
Chahat

WildFly 10, JCache - method caching

i have simple application using Spring Boot. I wanted allow method caching with JSR107 - JCache. So with help of tutorial i put together this code :
#CacheResult(cacheName = "testpoc")
public Country getCountry(Integer id){
System.out.println("---> Loading country with code '" + id + "'");
return new Country(id, "X", "Title");
}
with this POM file
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
...
(dependency 'spring-boot-starter-web' is there for simple REST service which call getCountry method)
Everything works like documentations says - method is invoked only once.
Now i wanted to try it on WildFly 10 application server
I have modified pom file :
excluded tomcat
exluded spring-boot-starter-cache
added infinispan-jcache (because i want to use cache configured / managed by wildfly in standalone/domain.xml)
Check pom file here on pastebin.
Problem is, that i am receiving following error :
Cannot find cache named 'java:jboss/infinispan/app-cache'
(i have tried to use both JNDI assigned and name to infinispan cache configured in wildfly).
Following code created Cache object (so i can used it) :
CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();
Cache<String, String> cache = cacheManager.createCache("testpoc", new MutableConfiguration<String, String>());
Question :
It is possible to use JCache method caching on WildFly 10 using Infinispan managed by WildFly ?
Or Infinispan should be used for method caching like JCache, hence JCache has "more functionality" than Infinispan.
Thank you very much
PS :It is not problem for me to put whole code on github and post link - it is few lines of code ...
There are a couple of problems with your approach so let me go through them in steps.
At first you need to use proper Infinispan setup. Infinispan bits shipped with WF should be considered as internal or private. In order to use Infinispan in your app properly - either add org.infinispan:infinispan-embedded to your deployment or install Infinispan Wildfly modules. You can find installation guide here (it's a bit outdated but still, the procedure is exactly the same - unpack modules into WF and use Dependencies MANIFEST.MF entry).
Once you have successfully installed Infinispan (or added it to your app), you need to consider whether you want to use Spring Cache or JCache. If you're only interested in using annotations - I would recommend the former since it's much easier to setup (all you need to do is to add #EnableCaching to one of your configurations). Finally with Spring Cache you will create an Infinispan CacheManager bean. An example can be found here.
Final note - if you still need to use JCache - use this manual to setup Caching Provider.

Injecting spring dependencies into Domain objects best practices?

I've scraped all over many resources, and have made this work and it's kinda complex, which turns me into asking for review and other ideas on how to properly inject spring dependencies into DomainObjects ..
My solution so far includes ..
Defining the dependencies needed for loadweaving
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
And then .. configure it in the spring context file :
<context:spring-configured />
<context:load-time-weaver/>
Using #Configurable for my domain classes :
#Configurable
public class MyDomainClass {
....
}
And of course, using these VM arguments :
-XX:-UseSplitVerifier -javaagent:C:/Users/albert/.m2/repository/org/springframework/spring-instrument/3.0.6.RELEASE/spring-instrument-3.0.6.RELEASE.jar
For this current solution, i have the feeling that this seems too much, like the lots of dependencies needed, and also the VM args, which i would dislike when deploying in production server where i have to use specific options, which i fear could be not supported in the future or perhaps have different behaviours between version.
Im thinking of doing domainObjects with prototype scope, but i fear the dependencies issues when fetching the domain objects from the database (not from applicationContext).
Please share your experiences, thank you !
1: When you start injection stuff dynamically into domain objects, they're really not domain objects any more in the sense that the domain should reflect your information model, independent of any business rules and functional logic.
2: Remember KISS (keep it simple...). At some point, someone else might have to take ownership and maintain your code so have some mercy on that person :)
I would call this an anti-pattern, which in my opinion should be avoided.
If you use compile time waving, then you will not need the VM argument.

Resources