How to add a Spring project to Tomcat configuration using Eclipse IDE, so that Tomcat will load the index page from web.xml - spring

I have a project imported as an existing Maven project in Eclipse, however I'm having trouble configuring Tomcat so that it loads the index page from web.xml
User Yugerten tried to help with their solution, however I'm obviously doing something wrong and its not running, therefore I include the screenshots of configuration and error log.

Add a plugin configuration to your pom.xml
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<path>/myWebApp</path>
</configuration>
</plugin>
...
</plugins>
...
</build>
eclipse
Or via Terminal / cmd:
mvn tomcat7:run / mvn tomcat7:deploy / mvn tomcat7:undeploy / mvn tomcat7:undeploy

Related

Spring Boot useTestClasspath throws CannotLoadBeanClassException and ClassNotFoundException

I have a maven project with a test class located at src/test/java/MyDevClass which is intended for development/testing purposes only. I would like to use it when I start spring-boot-maven-plugin with the command line mvn spring-boot:run.
So, my pom.xml contains:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- TODO: Will create a maven profile and have useTestClasspath only for development/testing -->
<useTestClasspath>true</useTestClasspath>
</configuration>
</plugin>
</plugins>
</build>
But, I get the following error:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [MyDevClass]
Caused by: java.lang.ClassNotFoundException: MyDevClass
Intriguing enough, I have another project using tomcat7-maven-plugin and it works fine:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<useTestClasspath>true</useTestClasspath>
</configuration>
</plugin>
What I am missing?
Spring-boot maven plugin does not include current module's test sources (and resources) into classpath even if useTestClasspath is set to true.
I ran a forked execution with verbose logging (-X flag to Maven), and the plugin listed the forked JVM classpath. Test classes were not included.
If you look at the plugin sources (version 1.5.3 at time of writing), the flag useTestClasspath is only used in the addDependencies method.
I see two options as workarounds
Add the target/test-classes dir to the run classpath:
<directories>
<directory>${project.build.testOutputDirectory}</directory>
</directories>
For older plugin version use:
<folders>
<folder>${project.build.testOutputDirectory}</folder>
</folders>
The problem here is that the folder is added to the beginning of classpath, while test files are preferable at its end.
Create another Maven module with test classes and resources, and add it as a <scope>test</scope> dependency.

how to start tomcat server automatically through tomcat 7 maven plugin

How to start a tomcat server which is present in my local through tomcat7 maven plugin...how to give the source of my local tomcat server in tomcat plugin so that if we give the tomcat7:run command...local server will run automcatically.
You must provide the url in the plugin configuration.
Note that for Tomcat 8 instances (I havent tested it in earlier tomcat versions) you need domain:port/manager/text
<plugins>
..
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<path>/examplePath</path>
</configuration>
</plugin>
..
</plugins>

Deploy Java EE Wildfly REST Application to Openshift

I am new to Openshift and having trouble with deploying my Java EE project to it. I have made REST API for a simple webstore. Locally it works fine on Wildfly 9.0.2 I want to deploy it on openshift. I 've made new wildfly9 + mysql5.5 application using eclipse openshit jboss plugin and added a profile to root pom.xml:
<profiles>
<profile>
<id>openshift</id>
<build>
<finalName>webstore</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<outputDirectory>deployments</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
My root project consist of several maven modules including store-ear (EAR), store-jpa (JAR), store-rest (WAR), store-web (WAR), store-services (EJB), store-rest-interfaces (JAR),store-service-interfaces (JAR).
I have changed datasourse in JPA configuration (persistence.xml) to use MysqlDB on Openshift.
After pushing back to openshift the build is succesfull, but when it gets deployed it is missing some dependancies (ClassNotFoundException), and fails to deploy main war file.
You use a maven-war plugin in your openshift maven profile.
But you say that your project is packaged as en ear. So you should probably deploy this ear which contains all your project modules (wars, ejbs, libs...) instead of a specific war of your project.
To achieve this, you have to use a maven-ear plugin instead of the maven-war one in your openshift profile which would look like this:
<profile>
<id>openshift</id>
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10</version>
<configuration>
<outputDirectory>deployments</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>

How to deploy Spring Boot application to different URL on Tomcat?

I am building and deploying my Spring Boot application into Tomcat with mvn tomcat:deploy and with this configuration:
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://127.0.0.1:8080/manager/text</url>
<server>tomcat</server>
<path>/${project.build.finalName}</path>
<username>admin</username>
<password>password</password>
</configuration>
</plugin>
</plugins>
</build>
Application runs then at /${project.artifactId}. I would like to deploy the application to the another URL, ideally to set target URL while I call Maven deploy command. Is it possible? If so, how can I achieve it?
You can override maven properties from command line with -D option.
To specify another url for your app the interesting properties are maven.tomcat.port and maven.tomcat.path.
The following command line should do the trick :
mvn -Dmaven.tomcat.port=8181 -Dmaven.tomcat.path=/custom tomcat:deploy

How to set server port with org.eclipse.jetty:jetty-maven-plugin?

I am currently setting the port via a jetty.xml file and I've been trying to figure out from the new documentation how to actually define an httpConnector through the Maven plugin's configuration. The docs on Eclipse's site seem a bit vague on it and I've been trying to figure this out for a while, thus ending up using a jetty.xml. I'd like to find out the proper way to do this now.
I'm currently using org.eclipse.jetty:jetty-maven-plugin:9.2.1.v20140609.
The jetty-maven-plugin documentation (for jetty 11 at the time of this answer - update) states that you can either configure the httpConnector element in the pom.xml file to setup the ServerConnector preferences or use the jetty.http.port system property to change the port or use the Jetty descriptor i.e. the way you are doing it actually.
Then you have several options:
(Java) System Property:
Change the port when just running your application through the mvn command:
mvn jetty:run -Djetty.http.port=9999
(Maven) Project Property:
Set the property inside your project pom.xml descriptor file:
<properties>
<jetty.http.port>9999</jetty.http.port>
</properties>
Then just run your application through the Jetty plugin and the port will be picked up automatically:
mvn jetty:run
(Maven) Jetty Plugin Configuration:
Set the port in your plugin declaration inside the pom.xml file:
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.1.v20140609</version>
<configuration>
<httpConnector>
<!--host>localhost</host-->
<port>9999</port>
</httpConnector>
</configuration>
</plugin>
</plugins>
</build>
EDIT
In new versions of jetty-maven-plugin, jetty.http.port is the default port property and jetty.port won't work as in previous plugin versions.
Run following command:
mvn jetty:run -Djetty.port=9999
I guess mvn jetty:run -Djetty.http.port=9999 is deprecated. It didn't work for me.
You may configure the port through the pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.1.v20140609</version>
<configuration>
<httpConnector>
<port>9999</port>
</httpConnector>
</configuration>
</plugin>
</plugins>
</build>
This works for me, confirmed as I am currently debugging the server in my chrome on port 8088.
mvn jetty:run -Dhttp.port=8088
By Default Jetty runs on 8080 port, if any application like oracle DB using that port in your system then Jetty server will not start and gives some BIND exception. to overcome this if your project is maven project then in pom.xml file use below code, then it works perfectly(here i am using port 8888 which is free in my system)
<!-- The Jetty plugin allows us to easily test the development build by
running jetty:run on the command line. -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.plugin.version}</version>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds>
<httpConnector>
<host>localhost</host>
<port>8888</port>
</httpConnector>
</configuration>
</plugin>
<connectors>
<connector>
<port>9999</port>
</connector>
</connectors>
in pom.xml file

Resources