Which profile does Intellij use when you do Run Application? - spring

I have two maven profiles.One is Dev which has a dependency on embedded Tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
and Prod which has not.
At the upper right corner of Intellij I can see the two profiles.
When I Run Application having checked the Dev profile, the app fails because it can't find the embedded Tomcat.
If I include the dependncy in the main profile and removing the scope tag,the app loads.
What am I doing wrong?
when you do Run Application from within Intellij under which profile does it run the application?

The provided scope means that this dependency is supposed to be provided by the external environment that runs your application, e.g. the application server, in case you deploy it as a war file. In your case, when you run it in the development environment, there is noone to provide this dependency, so specifying this scope probably doesn't make sense. This is the reason it works when you remove the scope element.

Related

Issue running Kogito DMN TrafficViolationTest in my junit

I imported dmn-quarkus-example project into eclipse. the project compiles and shows no errors. when I run mvn clean quarkus:dev, I was able to test the rest endpoint through postman and it works.
but my junit in my eclipse fails with 404.
I read few blogs and updated my application.properties file with the following, still no luck
quarkus.http.port=9090
%dev.quarkus.http.port=9191
quarkus.http.test-port=8181
the other thing I had to do to get my junit working is added the following dependencies in the pom.xml
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
it working still not able to understand why?
it started working after
mvn clean install and tried maven update on eclipse.....
it random....
This sounds like Eclipse is not configured properly, in general; based on the provided information not possible to even say if it's something related to Quarkus, or just a general Eclipse configuration issue.
Moreover, tested locally with Eclipse 2019-12 and no special configuration and no special plugin, running the JUnit test works out of the box correctly, in the example screenshot below running the unit test of the REST interface for Kogito on Quarkus example:
Please ensure you have no Maven exclusion in your Eclipse configuration, and that Eclipse is allowing to run the necessary Maven phases with M2Eclipse (like running any Maven project with Eclipse)

How do I specify a JAR in a POM such that it is excluded from packaging but available when running from within IntelliJ?

I have a project where several dependency versions need to be chosen at deployment time - i.e. specified in the classpath.
The provided scope prevents the dependency being packaged but the project fails when I try to run from within IntelliJ IDEA
e.g.
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.3.1</version>
<scope>provided</scope>
</dependency>
produces
{stacktrace ...}
Caused by: java.lang.ClassNotFoundException: javax.jms.ConnectionFactory
If I remove the scope the project runs fine but, of course, includes the jar.
If you mark a jar as <provided>, the classes need to be provided by the container that runs the surrounding war/ear.
When you run your project from within IntelliJ, it is probably deployed on some kind of container. Make sure this container provides you <provided> dependencies.
Final approach was to create two maven profiles, one for running locally and one for packaging. The local profile used compile scope while the package profile used provided.

Use Embedded MongoDB when active profile is "test"

I have
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
in my pom.xml so my Spring Boot application attempts to connect to MongoDB on localhost when the application is started.
I also have
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
so when I run unit tests the embedded MongoDb is used.
For the purpose of integration testing, I'd like to also use the embedded MongoDB if the application is started with the "test" active profile.
So effectively, I'd like to remove <scope>test</scope> from the embed dependency and have the the embed version used when the active profile is 'test'.
How can I achieve this?
Most probably the solution you are looking for is adding a profiled exclusion of embedded mongo database. Try to follow below steps:
Assume that default profile is for development purposes so you want to run embedded mongo in it. EmbeddedMongoAutoConfiguration is the class responsible for configuration of embedded mongo database. It is run when embedded mongo class is on classpath so just remove test scope as you pointed out
Create a profile for production, docker or whatever suits you and in this profile make sure you add following entry in yml (or properties file):
spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration
Please bear in mind that embedded mongo is on your classpath now which might be not desired for production environment, so you might wanna also configure maven to exclude it in some maven profile.
use spring profiles
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-set-active-spring-profiles
#ActiveProfiles("test")

Maven dependency management in IntelliJ

I'm currently building apps for Apache Spark. Spark provides during runtime a lot of dependencies, which I normally need if I test/run the apps locally in the IDE (IntelliJ).
Is there any possibility to have different set of dependendencies related if I use the 'package' or the usual compile/run target in IntelliJ ?
For instance, this is a needed dependency to Hadoop
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
<scope>provided</scope>
</dependency>
But the scope 'provided' does not work when I run it locally in the IDE.
If you want IntelliJ to use its own build process rather than Maven's, it's probably better to tell add a (global) library to your project dependencies in the IDE.
It definitely won't be providing these Spark JARs by default, which is what you're telling Maven here.

How is a war file created for spring boot with maven?

I'm trying to follow the guide for converting a spring project to a war.
http://spring.io/guides/gs/convert-jar-to-war/
It starts out using maven and gradle and then right after the jar portion it completely forgets about maven and only has gradle updates.
There are two main changes that you need to make in the pom. The first is to change the project's packaging type to war:
<groupId>org.springframework</groupId>
<artifactId>gs-convert-jar-to-war</artifactId>
<version>0.1.0</version>
<packaging>war</packaging>
The second is to add a dependency on spring-boot-starter-tomcat and mark it as provided:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
mvn package will now produce a war file that can be run using java -jar or deployed to a separate servlet container.
There is an official guide at spring:
http://spring.io/guides/gs/convert-jar-to-war-maven/
Pay attention to "Initialize the servlet" section.
It explains an important point of adding a class that substitutes web.xml. Without it (or without proper web.xml) you will get a war file but when deployed nothing will be accessible in browser as nothing will be registered as your request dispatcher.
Also note that it is best to run this example on Tomcat 8 as it supports latest servlet specs. I have spent number of hours trying to figure out why it does not work on my Tomcat 7.

Resources