Skip to content

Commit

Permalink
Repackage application as a runnable WAR
Browse files Browse the repository at this point in the history
The previous build created a fat JAR that could only be run from the command line. This commit modifies the Gradle configuration to build a WAR that can be run inside an application server. Command-line behaviour is preserved by the Spring Boot Gradle plugin which adds provided dependencies to the WAR.

  Resolves issue #31.
  • Loading branch information
emersonf committed Dec 22, 2015
1 parent a0f82f3 commit 447a405
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 40 deletions.
45 changes: 31 additions & 14 deletions shim-server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ buildscript {
}

ext {
springBootVersion = "1.3.0.RELEASE"
springBootVersion = "1.3.1.RELEASE"
}

dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}

apply plugin: 'war'
apply plugin: 'spring-boot'

version = shimmerVersion

jar {
war {
baseName = 'shimmer'
}

Expand All @@ -29,10 +30,6 @@ ext {
signpostVersion = '1.2.1.2'
}

configurations {
compile.exclude module: "spring-boot-starter-tomcat"
}

dependencies {
compile project(':java-shim-sdk')
compile "commons-io:commons-io:2.4"
Expand All @@ -49,33 +46,53 @@ dependencies {
compile "oauth.signpost:signpost-commonshttp4:${signpostVersion}"
compile "oauth.signpost:signpost-core:${signpostVersion}"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.springframework.boot:spring-boot-starter-jetty"
compile "org.springframework.data:spring-data-mongodb"
compile "org.springframework.security.oauth:spring-security-oauth"
compile "org.springframework.security.oauth:spring-security-oauth2"
compile "org.springframework:spring-web"
compile "org.springframework:spring-webmvc"
compile "javax.validation:validation-api:1.1.0.Final"
compile "org.codehaus.woodstox:woodstox-core-asl:4.4.1" // required to print XML

testCompile 'org.testng:testng:6.8.21' // slowly migrate to TestNG
testCompile "org.hamcrest:hamcrest-core"
testCompile "org.hamcrest:hamcrest-library"
testCompile "junit:junit"
testCompile "org.mockito:mockito-core"
testCompile "org.springframework.boot:spring-boot-starter-test"
testCompile "org.springframework:spring-test"
testCompile "org.testng:testng:6.8.21" // slowly migrate to TestNG

runtime "org.glassfish:javax.el"
runtime 'org.slf4j:jcl-over-slf4j'
runtime 'org.slf4j:log4j-over-slf4j'
runtime 'ch.qos.logback:logback-classic'
runtime "org.yaml:snakeyaml"

/* Servlet API and Jetty dependencies shouldn't be included in the `lib` directory of the assembled WAR file,
as they'll conflict with existing classes in the application server's classpath. To prevent them from being
copied into the WAR file, their scope is set to `providedCompile` or `providedRuntime` as defined
by the Gradle WAR plugin.
In order to make the WAR executable from the command line, the Spring Boot Gradle plugin copies the
dependencies to a different directory in the WAR file called `lib-provided`, which is ignored by the application
server but contributes classes to the classpath when running with `java -jar`.
Unfortunately, IntelliJ's Spring Boot run configuration doesn't include dependencies with provided scope in the
application classpath, and the application fails to start because of missing classes. This is tracked
at https://youtrack.jetbrains.com/issue/IDEA-107048. To get around this, you can either use a Gradle run
configuration and set it to execute the `bootRun` task. Or you can use the Spring Boot run configuration
and toggle the comments on the following four lines.
*/
providedCompile "javax.servlet:javax.servlet-api"
providedRuntime "org.springframework.boot:spring-boot-starter-jetty"
// compile "javax.servlet:javax.servlet-api"
// runtime "org.springframework.boot:spring-boot-starter-jetty"
}

task copyArchiveJarToDockerContext(dependsOn: assemble, type: Copy) {
task copyWarToDockerContext(dependsOn: assemble, type: Copy) {
from 'build/libs'
into 'docker'
include "${jar.archiveName}"
include "${war.archiveName}"
rename { String fileName ->
fileName.replace("${jar.archiveName}", "${jar.baseName}.jar")
fileName.replace("${war.archiveName}", "${war.baseName}.war")
}
}
build.dependsOn copyArchiveJarToDockerContext
build.dependsOn copyWarToDockerContext
4 changes: 2 additions & 2 deletions shim-server/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ MAINTAINER Emerson Farrugia <[email protected]>
ENV SERVER_PREFIX /opt/omh/shimmer

RUN mkdir -p $SERVER_PREFIX
ADD shimmer.jar $SERVER_PREFIX/
ADD shimmer.war $SERVER_PREFIX/
EXPOSE 8083

CMD /usr/bin/java -jar $SERVER_PREFIX/shimmer.jar --spring.config.location=file:$SERVER_PREFIX/
CMD /usr/bin/java -jar $SERVER_PREFIX/shimmer.war --spring.config.location=file:$SERVER_PREFIX/
25 changes: 10 additions & 15 deletions shim-server/src/main/java/org/openmhealth/shimmer/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import static org.openmhealth.schema.configuration.JacksonConfiguration.newObjectMapper;

Expand All @@ -34,22 +34,17 @@
*/
@SpringBootApplication
@EnableConfigurationProperties
@EnableMongoRepositories("org.openmhealth.shim") // FIXME confirm
@ComponentScan(basePackages = {"org.openmhealth.shim", "org.openmhealth.shimmer"}) // FIXME confirm
public class Application extends WebSecurityConfigurerAdapter {
@EnableMongoRepositories("org.openmhealth.shim")
@ComponentScan(basePackages = {"org.openmhealth.shim", "org.openmhealth.shimmer"})
public class Application extends SpringBootServletInitializer {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

// TODO refactor authentication
@Override
protected void configure(HttpSecurity http) throws Exception {
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}

/**
* Allow full anonymous authentication.
*/
http.csrf().disable().authorizeRequests().anyRequest().permitAll();
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

// TODO look into Jackson2ObjectMapperBuilder to support Spring Boot configuration, e.g. for indentation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.openmhealth.shimmer.common.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


/**
* TODO this is just a refactoring of existing code, needs to be revised
*
* @author Emerson Farrugia
*/
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {

/**
* Allow full anonymous authentication.
*/
http.csrf().disable().authorizeRequests().anyRequest().permitAll();
}
}
14 changes: 5 additions & 9 deletions shim-server/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
spring:
application:
name: Open mHealth shim server
# this is necessary to prevent proxy errors due https://github.com/spring-projects/spring-boot/issues/1929
dao:
exceptiontranslation:
enabled: false
name: Shimmer
data:
mongodb:
uri: mongodb://mongo:27017/omh_dsu
Expand All @@ -17,16 +13,16 @@ security:
basic:
enabled: false
logging:
file: omh-shims.log
file: shimmer.log

openmhealth:
shim:
server:
callbackUrlBase: http://localhost:8083

#NOTE: Un-comment and fill in the clientId/clientSecret with your credentials if you're not using the UI
#NOTE: Un-comment and set partnerAccess to true if your credentials for a given API have partner access
#NOTE: Un-comment and fill in your serialValues for iHealth, otherwise the iHealth shim will not work correctly
#NOTE: Uncomment and fill in the clientId/clientSecret with your credentials if you're not using the UI
#NOTE: Uncomment and set partnerAccess to true if your credentials for a given API have partner access
#NOTE: Uncomment and fill in your serialValues for iHealth, otherwise the iHealth shim will not work correctly
#ihealth:
# serialValues:
# SC: [YOUR_SC_VALUE]
Expand Down

0 comments on commit 447a405

Please sign in to comment.