Permalink
Browse files

Package application to standalone jar with embedded jetty

  • Loading branch information...
Pauli Kärpänoja
Pauli Kärpänoja committed Dec 4, 2015
1 parent 29625cb commit b7404c181328a8c2118f44ca6e2f5406c7780837
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,15 @@
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %d [%t] %c -- %m%n
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.file.File=logs/app.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p %d [%t] %c -- %m%n
# Log SQL statements:
#log4j.logger.com.mysema.query.sql.AbstractSQLQuery=DEBUG
log4j.rootLogger = INFO, file, console
log4j.logger.org.springframework=WARN
View
105 pom.xml
@@ -8,7 +8,7 @@
<artifactId>initiative-web</artifactId>
<name>Aloitepalvelu</name>
<version>3.5.1-SNAPSHOT</version>
<packaging>war</packaging>
<packaging>jar</packaging>
<properties>
<finalName>root</finalName>
@@ -19,7 +19,7 @@
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
<!-- Dependency Versions -->
<jetty.version>7.5.4.v20111024</jetty.version>
<jetty.version>9.3.6.v20151106</jetty.version>
<spring.version>3.1.2.RELEASE</spring.version>
<querydsl.version>3.3.0</querydsl.version>
<postgresql.version>9.1-901-1.jdbc4</postgresql.version>
@@ -59,6 +59,12 @@
<version>${jmockit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5-rc1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
@@ -110,12 +116,6 @@
<artifactId>jackson-databind</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
@@ -143,25 +143,21 @@
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.6</version>
<scope>provided</scope>
</dependency>
<!-- Hibernate Validator -->
@@ -287,19 +283,16 @@
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
@@ -341,6 +334,21 @@
<include>pdf/*</include>
</includes>
</resource>
<resource>
<!-- Minify plugin exports minified css files under target/tmp.
Filtering plugin copies them from target back to src -->
<!-- Filtering plugin was not able to filter files if sourcedir=targetdir -->
<directory>${project.build.directory}/tmp/css</directory>
<includes>
<include>*.css</include>
</includes>
<filtering>true</filtering>
<targetPath>${basedir}/src/main/webapp/css</targetPath>
</resource>
<resource>
<targetPath>src/main/webapp</targetPath>
<directory>src/main/webapp</directory>
</resource>
</resources>
<testResources>
<testResource>
@@ -349,6 +357,55 @@
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.artifactId}-standalone</finalName>
<transformers>
<!-- http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html -->
<!-- http://stackoverflow.com/questions/10584752/spring-3-namespacehandler-issues-with-maven-shade-plugin -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer">
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer">
<addHeader>false</addHeader>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>fi.om.initiative.server.RunJetty</Main-Class>
<!--<Build-Number>${buildNumber}</Build-Number> &lt;!&ndash; Git revision &ndash;&gt;-->
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
@@ -443,7 +500,7 @@
<id>default-minify</id>
<phase>process-resources</phase>
<configuration>
<webappTargetDir>${basedir}/src/main/webapp</webappTargetDir>
<webappTargetDir>target/tmp</webappTargetDir>
<cssSourceDir>css</cssSourceDir>
<cssSourceFiles>
<cssSourceFile>normalize.css</cssSourceFile>
@@ -474,7 +531,7 @@
<id>iframe-minify</id>
<phase>process-resources</phase>
<configuration>
<webappTargetDir>${basedir}/src/main/webapp</webappTargetDir>
<webappTargetDir>target/tmp</webappTargetDir>
<cssSourceDir>css</cssSourceDir>
<cssSourceFiles>
<cssSourceFile>normalize.css</cssSourceFile>
@@ -488,20 +545,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/webapp/css</directory>
<targetPath>css</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -2,6 +2,7 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.mysema.query.sql.postgres.PostgresQueryFactory;
import fi.om.initiative.conf.AppConfiguration.AppDevConfiguration;
import fi.om.initiative.conf.AppConfiguration.ProdPropertiesConfiguration;
@@ -22,6 +23,8 @@
import org.joda.time.Period;
import org.joda.time.format.ISOPeriodFormat;
import org.joda.time.format.PeriodFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
@@ -31,6 +34,7 @@
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
import org.springframework.jdbc.support.SQLExceptionTranslator;
@@ -41,8 +45,12 @@
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.servlet.SessionCookieConfig;
import java.io.File;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
@@ -58,6 +66,8 @@
@EnableScheduling
public class AppConfiguration {
private static Logger logger = LoggerFactory.getLogger(AppConfiguration.class);
@Inject Environment env;
@Resource JdbcConfiguration jdbcConfiguration;
@@ -69,6 +79,8 @@
private PeriodFormatter periodFormatter = ISOPeriodFormat.standard();
@Inject ServletContext servletContext;
/**
* PRODUCTION PROPERTIES CONFIGURATION: encrypted app.properties
*/
@@ -79,7 +91,12 @@
@Bean
public static EncryptablePropertiesConfigurer propertyProcessor() {
return new EncryptablePropertiesConfigurer(new ClassPathResource("app.properties"));
File appProperties = new File("config/app.properties");
if (!appProperties.exists()) {
logger.warn("config/app.properties not found: \n USING DEFAULT PROPERTIES!");
}
return new EncryptablePropertiesConfigurer(new FileSystemResource(appProperties));
}
}
@@ -390,4 +407,16 @@ public SupportStatementPdfGenerator supportStatementPdfGenerator(ResourceLoader
pdf_sv
);
}
@PostConstruct
public void setSecureCookie() {
boolean disableSecureCookie = Sets.newHashSet(env.getActiveProfiles()).contains("disableSecureCookie");
SessionCookieConfig sessionCookieConfig = servletContext.getSessionCookieConfig();
// servletContext is mocked in integrationTests so it will return null.
if (sessionCookieConfig != null) {
sessionCookieConfig.setSecure(!disableSecureCookie);
}
}
}
@@ -0,0 +1,23 @@
package fi.om.initiative.conf;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Paths;
public class ConfigurationFileLoader {
public static File getFile(String fileName) throws FileNotFoundException {
File jarLocation = Paths.get("").toFile();
File configDir = new File(jarLocation.getAbsolutePath() + "/config/");
if (!configDir.isDirectory()) {
throw new FileNotFoundException("Config dir not found: " + configDir.getAbsolutePath());
}
File file = new File(configDir.getAbsolutePath() + "/" + fileName);
if (!file.exists()) {
throw new FileNotFoundException("Configuration file not found: " + file.getPath());
}
return file;
}
}
@@ -1,5 +1,6 @@
package fi.om.initiative.conf;
import com.jolbox.bonecp.BoneCPConfig;
import com.jolbox.bonecp.BoneCPDataSource;
import com.mysema.query.sql.PostgresTemplates;
import com.mysema.query.sql.SQLTemplates;
@@ -14,6 +15,7 @@
import fi.om.initiative.dto.author.AuthorRole;
import fi.om.initiative.dto.initiative.InitiativeState;
import fi.om.initiative.util.ReviewHistoryType;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
@@ -26,7 +28,8 @@
import javax.inject.Inject;
import javax.inject.Provider;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
@Configuration
@@ -48,21 +51,30 @@ public SQLTemplates templates() {
}
/**
* Default connection pool settings are defined in <tt>classpath:/bonecp-default-config.xml</tt>.
*
* Deployment specific overrides in <tt>classpath:/bonecp-config.xml</tt>, e.g. <tt>${jetty.home}/resources/bonecp-config.xml</tt>.
* Environment specific overrides in <tt>config/bonecp-config.xml</tt>.
* @return
*/
@Bean
public DataSource dataSource() {
BoneCPDataSource dataSource = new BoneCPDataSource();
BoneCPDataSource dataSource;
try {
File file = ConfigurationFileLoader.getFile("bonecp-config.xml");
log.info("Using bonecp-config: " + file.getAbsolutePath());
try (FileInputStream xmlConfigFile = FileUtils.openInputStream(file)) {
dataSource = new BoneCPDataSource(new BoneCPConfig(xmlConfigFile, null));
}
} catch (Exception e) {
dataSource = new BoneCPDataSource();
log.error("Unable to initialize bonecp-config.xml. Using default bonecp-settings.", e);
}
dataSource.setDriverClass(env.getRequiredProperty(PropertyNames.jdbcDriver));
dataSource.setJdbcUrl(env.getRequiredProperty(PropertyNames.jdbcURL));
dataSource.setUsername(env.getRequiredProperty(PropertyNames.jdbcUser));
dataSource.setPassword(env.getRequiredProperty(PropertyNames.jdbcPassword));
log.info(dataSource.getConfig().getConfigFile());
log.info(dataSource.toString());
return dataSource;
}
@Bean
Oops, something went wrong.

0 comments on commit b7404c1

Please sign in to comment.