I'm using FlywayDB for migrations on a Maven Java project. I am currently working to make it possible to deploy to Heroku.
On my local machine I am using the Maven Flyway plugin to run migrations:
$> mvn clean compile flyway:migrate
To do the same on heroku, I would normally try:
$> heroku run mvn flyway:migrate
However, mvn is not available after the build phase, so this yields an error (mvn: command not found)
How can I run my flyway migrations on Heroku?
I think your best bet is to create a small class in your application that uses the FlywayDB Java API. It might look like this:
class Migrator {
public static void main(String[] args) throws Exception {
...
Flyway flyway = new Flyway();
flyway.setDataSource(url, user, password);
flyway.migrate();
}
}
Then create an entry in your Procfile like this:
migrate: java -cp target/classes:target/dependency/* Migrator
And finally run it as needed with heroku run migrate.
The reason Heroku does not include Maven in the slug (i.e. at runtime) is because the .m2 directory is not retained. If Maven were included, and you then ran a mvn command, it would first have to download the internet. The .m2 directory is not retained because it would make the slug size too large.
According to the Heroku documentation using the Maven plugin is not recommended for running Flyway migrations.
Within the documentation there are two alternatives:
Running Flyway automatically with Spring
Running Flyway with
the Java API (Also see #codefinger's answer)
Related
I have a Spring project to which I have made two very basic changes:
logger.info("testing changes made");
return new ResponseEntity<>(HttpStatus.NO_CONTENT); //previously HttpStatus.OK
I then:
git committed the changes (not really necessary)
mvn clean install -P docker-build-image
docker tag my-service:old-tag my-service:new-tag
I then used this image to install the service in a kubernetes pod as follows:
echo "🟢 deploying auth service"
helm uninstall auth
kubectl delete pod -l app.kubernetes.io/name=auth
kind load docker-image my-service:new-tag
helm install auth ./auth --wait
The trouble is: I am never getting any changes. I only see the output from the old version of the application.
What is going on here? This is very strange to me. This feels very much like a maven|docker plugin kind of problem. Or, is it a kubernetes|kind|helm problem?
mvn install will just install the package.
You should do mvn package before install to make new jar file.
The problem I was experiencing resulted from the docker-building module was using an old snapshot to build the docker image. The wrong dependency was being used in the POM file of that module. I changed the dependency and everything worked just fine.
I created spring + angular + gradle with Jhipster.
I first do
npm install
Then , I run this command:
./gradlew -Pprod -Pwar clean bootWar
which reads gradle/war.gradle:
apply plugin: "war"
bootWar {
mainClassName = "com.bpn.legolas.ExtractAccountApp"
includes = ["WEB-INF/**", "META-INF/**"]
webXml = file("${project.rootDir}/src/main/webapp/WEB-INF/web.xml")
}
war {
webAppDirName = "build/resources/main/static/"
webXml = file("${project.rootDir}/src/main/webapp/WEB-INF/web.xml")
enabled = true
archiveExtension = "war.original"
includes = ["WEB-INF/**", "META-INF/**"]
}
and it creates a war. But when i deploy to another machine, to tomcat, i got the error page:
An error has occured :-( Usual error causes You started the
application from an IDE and you didn't run npm start or npm run
webpack:build. You had a network error while running npm install. If
you are behind a corporate proxy, it is likely that this error was
caused by your proxy. Have a look at the JHipster error logs, you will
probably have the cause of the error. You installed a Node.js version
that doesn't work with JHipster: please use an LTS (long-term support)
version, as it's the only version we support. Building the client side
code again If you want to go fast, run ./mvnw to build and run
everything.
If you want to have more control, so you can debug your issue more
easily, you should follow the following steps:
Install npm dependencies with the command npm install Build the client
with the command npm run webpack:build or npm start Start the server
with ./mvnw or using your IDE Getting more help If you have a question
on how to use JHipster Go to Stack Overflow with the "jhipster" tag.
If you have a bug or a feature request First read our contributing
guidelines.
Then, fill a ticket on our bug tracker, we'll be happy to resolve your
issue!
If you want to chat with contributors and other users Join our chat
room on Gitter.im. Please note that this is a public chat room, and
that we expect you to respect other people and write in a correct
English language!
For same steps, i export as jar.
First
npm install
Then
./gradlew -Pprod -Pjar clean bootJar
Than at that machine, i do
java -jar xxxx.jar
and it works! But for war, as i said before, it does not work. It says about npm install orwebpack`
Please help me about it.
STEPS
1- My war name is halil-0.0.1-SNAPSHOT.war
2- I put to webapps under tomcat
3- Then i go to:
http://localhost:8080/halil-0.0.1-SNAPSHOT/
4- I see the errors which i put in the screenshot.
Which i posted as text in this question
5- Javascript console shows those errors:
6- Network shows those errors:
7- When i go to
http://localhost:8080/halil
or
http://localhost:8080/
it brings 404 page
I had the same problem when I was trying to migrate from Angular 4 to Angular 12 in my Jhipster project.
The issue was that the static folder was not being created before creating the war:
gradle script was wrong (the npm build was running in the correct step before the war)
So I have solved it following these steps:
Run npm build, so the static folder was created (you must be sure the exact path for your static folder is correct)
Check if the static folder was created in the path your war is expecting
Run ./gradlew -Pprod -Pwar clean bootWar
I have a spring-boot application which I need to start up by going to the folder directory and start up my web application via command line. I have a class called Application.java and the code inside it is as followed.
#SpringBootApplication(scanBasePackages = {"com.ubs.tas.topcat.dashboard"})
public class Application extends SpringBootServletInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class.getName());
private static final Class<Application> applicationClass = Application.class;
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
public static void main(String[] args) {
LOGGER.info("Starting...");
SpringApplication.run(Application.class, args);
}
}
I set up classpath then tried to run the command "java ApplicationUtility" but I'm getting this error message "Could not find the main class: ApplicationUtility. Program will exist."
I presume you are trying to compile the application and run it without using an IDE.
I also presume you have maven installed and correctly added maven to your environment variable.
To install and add maven to environment variable visit Install maven if you are under a proxy check out add proxy to maven
Navigate to the root of the project via command line and execute the command
mvn spring-boot:run
The CLI will run your application on the configured port and you can access it just like you would if you start the app in an IDE.
Note: This will work only if you have maven added to your pom.xml
You will need to build the jar file first. Here is the syntax to run the main class from a jar file.
java -jar path/to/your/jarfile.jar fully.qualified.package.Application
If you're using gradle, you can use:
./gradlew bootRun
Run Spring Boot app using Maven
You can also use Maven plugin to run your Spring Boot app. Use the below example to run your Spring Boot app with Maven plugin:
mvn spring-boot:run
Run Spring Boot App with Gradle
And if you use Gradle you can run the Spring Boot app with the following command:
gradle bootRun
To run the spring-boot application, need to follow some step.
Maven setup (ignore if already setup):
a. Install maven from https://maven.apache.org/download.cgi
b. Unzip maven and keep in C drive (you can keep any location. Path location will be changed accordingly).
c. Set MAVEN_HOME in system variable.
d. Set path for maven
Add Maven Plugin to POM.XML
`<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
`
Build Spring Boot Project with Maven
mvn package
or
mvn install / mvn clean install
Run Spring Boot app using Maven:
mvn spring-boot:run
[optional] Run Spring Boot app with java -jar command
java -jar target/mywebserviceapp-0.0.1-SNAPSHOT.jar
1.Run Spring Boot app with java -jar command
To run your Spring Boot app from a command line in a Terminal window you can use java -jar command.
This is provided your Spring Boot app was packaged as an executable jar file.
java -jar target/app-0.0.1-SNAPSHOT.jar
2.Run Spring Boot app using Maven
You can also use Maven plugin to run your Spring Boot app. Use the below command to run your Spring Boot app with Maven plugin:
mvn spring-boot:run
3.Run Spring Boot App with Gradle
And if you use Gradle you can run the Spring Boot app with the following command:
gradle bootRun
Spring Boot provide the plugin with maven.
So you can go to your project directory and run
mvn spring-boot:run
This command line run will be easily when you're using spring-boot-devs-tool with auto reload/restart when you have changed you application.
A Spring Boot project configured through Maven can be run using the following command from the project source folder
mvn spring-boot:run
For macOS user
If you configured your application using maven
First, go to your project directory in the terminal
then simply run
./mvnw spring-boot:run
You are done. Now you can hit localhost with port number.
This command mentioned in the spring official guide.
One of the ways that you can run your spring-boot application from command line is as follows :
1) First go to your project directory in command line [where is your project located ?]
2) Then in the next step you have to create jar file for that, this can be done as
mvnw package [for WINDOWS OS ] or ./mvnw package [for MAC OS] , this will
create jar file for our application.
3) jar file is created in the target sub-directory
4)Now go to target sub directory as jar was created inside of it , i.e cd target
5) Now run the jar file in there.
Use command java -jar name.jar [ name is the name of your created jar file.]
and there you go , you are done . Now you can run project in browser,
http://localhost:port_number
For Maven based projects use this command to run the spring boot application:
mvn spring-boot:run
I was having the same problem, I fixed it by modifying the dependency in the pom. I added web at the end.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-**starter**</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
open cmd from your project path the then at cmd run following cmmands-
1st way to build and run->
mvn clean install
for run only ->
mvn spring-boot:run
1st way to build and run after jar file generated in target folder ->
java -jar target/accounts-0.0.1-SNAPSHOT.jar
I have created an API using Ratpack and Groovy. All the GET, POST apps are working locally. Now I want this to move it to some remote server(say dev environment). How can I do this?
To start and run the app in local, I have to do either "gradle run" or "Run the Ratpack.groovy as a groovy script" from eclipse IDE. Then it says "Ratpack Server running in localhost:8080". And then I can use the APIs as localhost:8080/api/.../.../... but at the same time when I try to run it as JAVA Application, I am getting error as:
{"#timestamp":"2016-06-02T14:47:06.026+05:30","#version":1,"message":"Starting server...","logger_name":"ratpack.server.RatpackServer","thread_name":"main","level":"INFO","level_value":20000,"tags":null}
Exception in thread "main" java.io.UncheckedIOException: java.io.IOException: Is a directory
at ratpack.util.Exceptions.uncheck(Exceptions.java:52)
at ratpack.groovy.Groovy.ratpack(Groovy.java:112)
at ratpack.groovy.Groovy$ratpack.callStatic(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:206)
at Ratpack.run(Ratpack.groovy:22)
To deploy it in prod, I moved the jar to the server. After that what should I do to start the Ratpack server or rather API service?
I would recommend setting up a CI pipeline that builds a java jar and deploys the artifact to your target environment.
I recommend using the Shadow Plugin from John Engleman https://plugins.gradle.org/plugin/com.github.johnrengelman.shadow
This plugin produces a shadow jar (similar to Maven's shaded jar) that can optionally include bash scripts or batch scripts for starting your application.
High level suggestion:
Add the shadow plugin to your gradle build file
plugins {
id 'io.ratpack.ratpack-groovy' version '1.3.3'
id 'com.github.johnrengelman.shadow' version '1.2.3'
}
Have your CI server or you manually execute `gradlew installShadowApp
SCP/FTP this artifact from your build/installShadow directory to your target server
Invoke the shell script from build/installShadow/$appName/bin/$appName to start application
For a more maintainable solution I would recommend registering this shell script as a service or with a solution like monit/upstart/ etc
For a more detailed example on deploying to Heroku as an example take a look at my notes: http://danhyun.github.io/2016-gr8confeu-rapid-ratpack-groovy/#deploying_to_heroku
I am playing to Maven and tried to built a simple HellowWorld application.
This application uses Spring to libraries.
When I tried to run it, I run it through:
\target\classes
with command:
java -cp HelloWorldApp
It has a long list of classpath dependencies.
I think maven must have some more clever ways to do this instead of listing a whole list of dependency libs.
Can someone help?
Update:
Thanks. I now have another question. I run the project using:
mvn exec:java -Dexec.mainClass="com.vaannila.HelloWorldApp"
However, my project uses a Spring config called beans.xml which is in the
\src\main\resources
When I run it, it says:
Caused by: java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:142)at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.jav
a:336)
How can I specify where to look the Spring config?
Many thanks
Run your application by issuing mvn exec:java at the command line, maven will take care of the rest including download of the maven exec plugin.
EDIT As for your updated question: It appears that maven did not copy your resources to the target folder, you can use the maven-resources-plugin to do that. This link should help you get this done.
If you use IDE, such as eclipse +m2eclipse - it will calculate all dependencies from maven dependencies.
If you are running from command line use Exec maven plugin