Exclude entire class if no starter present - spring

Is there a way to exclude a class if there is no Spring Boot starter-X in pom. For example, if someone forgets to import a dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>-->
and I have a component that creates a dependency object on that starter. Is there a way to exclude the entire class and complain that there is no such file/class. I would like to compile the rest of the application without this class? Something like
#EnabledIf(value = "${is-present}")

Related

Micronaut Swagger multimodule

I am trying to visualize the API definitions from a Multimodule project. First I want to start from simple, and I want to visualize the yaml from the parent module. Then I will add other APIs in my Swagger Controller to call the specific url as is done in the example https://github.com/frehov/micronaut-swagger-server
I have my Swagger Controller and my SwaggerConfiguration with my index.hbs inside resource > views.swagger I took the example from the repo https://github.com/frehov/micronaut-swagger-server
This is my Result:
Instead of something like:
When I compared the two project, somehow my view.swagger package from my target folder gets generated into a different way, compared with the example:
What I am missing?
Could you please look at my repo:
https://github.com/amhg/swagger
Thank you
The folder containing the swagger-ui-wrapper (index.hbs) in your project is called 'views.swagger', while the sample has defined 'views/swagger' (so swagger is a folder inside of views). Change that and you get the views/swagger result in your target.
You also need to enable the micronaut handlebars-views (i.e. serving the .hbs files from a controller); add these to the pom.xml of startup (not at the root-pom!):
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-views</artifactId>
</dependency>
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
<version>4.1.0</version>
<scope>runtime</scope>
</dependency>
These will perform the magic of rendering the configuration you return from the controller into an html-page.
Depending on where you're going to put the swagger-annotated controllers, you might also want to add
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger.version}</version>
<scope>compile</scope>
</dependency>

Where to use which scope in maven pom.xml

I have a project "framework-ws" that contains 3 modules (api, dictionary, webapp).
This project is used has a parent project for multiples other web-services project.
For example, the project "core-ws" has "framework-ws" for parent. It also contains 3 modules (api, dictionary, webapp).
Each of these modules has the framework as dependency.
core-ws-api => framework-ws-api
core-ws-dictionary => framework-ws-dictionary
core-ws-webapp => framework-ws-webapp
(subproject => dependency)
Now I have other dependencies (lombok for example) that is used in every project (framework + child-project).
I don't understand where I need the declare this dependency.
In the parent project with a "provided" scope, and in each
child-project without scope
In the parent project with no scope, and no dependency in each
child-project
Another solution that I didn't think of
The second solution seems cleaner because I don't need to duplicate the dependencies in each pom.xml. But I don't know if it's the best practice.
EDIT : Here is a picture of projects structure.
In your parent pom use a dependency management section see https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</dependencyManagement>
Then in your child pom you will use the dependency but not specify a version, i.e. the version will be specified in the parent once for all children. You still need to include the dependency in each module.
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies>
In terms of scope read this https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope the default scope is compile which is what you will usually use. I think you are confused about the meaning of scope from readinf your question.

Using Spring autowired bean from other project in JUnit

I'm writing unit tests for my app consisting of several projects. I have project A for which I'm writing tests, and project B where I want to store some needed beans to be autowired in A test classes.
Prject A also needs B in the compilation scope.
If using dependency like that:
...
A/pom.xml
...
<dependency>
<artifactId>B</artifactId>
<scope>compile</scope>
<dependency>
spring is unable to autowire beans of B project. It's strange, because according to the Maven docs, compile scope also makes project content available at the stage phase.
In case I use the save dependency but with test scope, unit tests work, but app itself faild (pretty predictable).
In case when I use both dependencies, like:
...
A/pom.xml
...
<dependency>
<artifactId>B</artifactId>
<scope>compile</scope>
<dependency>
<dependency>
<artifactId>B</artifactId>
<scope>compile</scope>
<type>test-jar</type>
<scope>test</scope>
<dependency>
mvn clean install fails since it's unable to resolve dependencies.
So what can I do? Is there any best practise of using other project beans in unit tests in Spring?
The default maven scope (compile) should do it. Should give you access to the classes at compile/test time.
You should look at:
what packaging do you use for each project (jar/war)
how are you autowiring beans from B to A, does the autowiring works at runtime,
maybe there is a difference on how you create the Spring context at test time VS runtime
could be that Spring auto-scan mechanism doesn't scan the B project packages (at least in tests)
We usually do it like :
<dependency>
<artifactId>B</artifactId>
<groupId>groupID</groupId>
<version>version number</version>
<scope>compile</scope>
<type>jar or war</type>
</dependency>

#Webservice annotation exception on weblogic

I am trying to run my application which contain JAX WS (2.1) Webservice using JDeveloper 11g R2(11.1.2.3.0) in JDK 1.6.0_31-b05. The error is coming from #WebService annotation present on the class.
When I am running the application, I am getting below error,
java.lang.IllegalArgumentException: Argument(s) "type" can't be null.
at com.sun.xml.bind.api.TypeReference.<init>(TypeReference.java:89)
at com.sun.xml.ws.model.RuntimeModeler.processDocWrappedMethod(RuntimeModeler.java:758)
at com.sun.xml.ws.model.RuntimeModeler.processMethod(RuntimeModeler.java:678)
at com.sun.xml.ws.model.RuntimeModeler.processClass(RuntimeModeler.java:428)
at com.sun.xml.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:277)
at com.sun.xml.ws.server.EndpointFactory.createSEIModel(EndpointFactory.java:363)
at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:202)
at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:496)
at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:539)
at weblogic.wsee.jaxws.JAXWSDeployedServlet.getEndpoint(JAXWSDeployedServlet.java:183)
It seems that embedded Web Logic is using the internal libraries instead of provided one from JDK. The classes RuntimeModeler or TypeReference are present in JDK rt.jar starts with package com.sun.xml.ws.internal. Weblogic is picking these classes from glassfish.jaxb_1.0.0.0_2-1-12.jar & glassfish.jaxws.rt_1.2.0.0_2-1-5.jar, but these jars are not part of my application.
I have already used weblogic.xml with below tag,
<prefer-web-inf-classes>true</prefer-web-inf-classes>
I tried adding jaxws-api.jar & jws-api.jar in DefaultDomain/lib directory, but that didn't work
Any clue how to resolve this exception or how to force weblogic to use jdk runtime classes? The same application work properly on stand alone weblogic.
I had the same problem and found the answer here: http://www.intropro.com/resources/blog/66-argument-s-type-can-t-be-null
In short - the problem appears because you have jaxb-impl in you classpath which overrides WebLogics own jaxb, You may not explicitly refer to this dependency from your pom.xml, but some of your other dependencies do.
In my case I had apache-cxf as maven dependency and it had jaxb 2.1.13 as sub-dependency with scope "compile". All I had to do is exclude this apaches jaxb and add my own dependency with scope "provided" to explicitly use WebLogics jaxb.
in pom.xml it looked like this:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.2</version>
<exclusions>
<exclusion>
<artifactId>jaxb-impl</artifactId>
<groupId>com.sun.xml.bind</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<scope>provided</scope>
<version>2.1.13</version>
</dependency>
You can use eclipses "Dependency Hierarchy" tab in pom.xml view or simply command line "mvn dependency:tree" to find out how jaxb-impl made it to your classpath.
In my case, i had a typo in the arguments of the operation, where two arguments had the same webParam name. Modified that and deployed, issue was resolved.
Have you tried listing the correct jar in the manifest class-path: attribute? You could also put the jdk classes in the app and try using a FilteringClassLoader to specify which classes to use from the app rather than system classloader.
http://docs.oracle.com/cd/E15051_01/wls/docs103/programming/classloading.html#wp1097263

Cannot find SerialAddress class in Apache Mina 2.0.2

I added the below dependencies in my project POM file and the SerialAddress class is no where to be found from the downloaded mina-core.2.0.2.jar.
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId>
<version>2.0.2</version>
</dependency>
the package org.apache.mina.transport.serial doesnt even exist. Please advice me on the correct Dependency.
It looks like this class is not part of mina-core. Some exploration lead to the existence of Apache Mina Serial Communication Support.
So I guess you would want to add the dependency for mina-transport-serial.
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-transport-serial</artifactId>
<version>2.0.2</version>
</dependency>

Resources