Skip to content

Commit d102aad

Browse files
author
AWS CodeStar
committed
Initial commit made by AWS CodeStar during project creation.
0 parents  commit d102aad

File tree

17 files changed

+741
-0
lines changed

17 files changed

+741
-0
lines changed

README.md

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
Welcome to the AWS CodeStar sample web application
2+
==================================================
3+
4+
This sample code helps get you started with a simple Java web application
5+
deployed by AWS CodeDeploy to an Amazon EC2 server.
6+
7+
What's Here
8+
-----------
9+
10+
This sample includes:
11+
12+
* README.md - this file
13+
* appspec.yml - this file is used by AWS CodeDeploy when deploying the web
14+
application to EC2
15+
* buildspec.yml - this file is used by AWS CodeBuild to build the web
16+
application
17+
* pom.xml - this file is the Maven Project Object Model for the web application
18+
* src/main - this directory contains your Java service source files
19+
* src/test - this directory contains your Java service unit test files
20+
* scripts/ - this directory contains scripts used by AWS CodeDeploy when
21+
installing and deploying your application on the Amazon EC2 instance
22+
23+
24+
Getting Started
25+
---------------
26+
27+
These directions assume you want to develop on your local computer, and not
28+
from the Amazon EC2 instance itself. If you're on the Amazon EC2 instance, the
29+
virtual environment is already set up for you, and you can start working on the
30+
code.
31+
32+
To work on the sample code, you'll need to clone your project's repository to your
33+
local computer. If you haven't, do that first. You can find instructions in the
34+
AWS CodeStar user guide.
35+
36+
1. Install maven. See https://maven.apache.org/install.html for details.
37+
38+
2. Install tomcat. See https://tomcat.apache.org/tomcat-7.0-doc/setup.html for
39+
details.
40+
41+
3. Build the application.
42+
43+
$ mvn -f pom.xml compile
44+
$ mvn -f pom.xml package
45+
46+
4. Copy the built application to the Tomcat webapp directory. The actual
47+
location of that directory will vary depending on your platform and
48+
installation.
49+
50+
$ cp target/ROOT.war <tomcat webapp directory>
51+
52+
4. Restart your tomcat server
53+
54+
5. Open http://127.0.0.1:8080/ in a web browser to view your application.
55+
56+
What Do I Do Next?
57+
------------------
58+
59+
Once you have a virtual environment running, you can start making changes to
60+
the sample Java web application. We suggest making a small change to
61+
/src/main/webapp/WEB-INF/views/index.jsp first, so you can see how changes
62+
pushed to your project's repository are automatically picked up by your project
63+
pipeline and deployed to the Amazon EC2 instance. (You can watch the pipeline
64+
progress on your project dashboard.) Once you've seen how that works, start
65+
developing your own code, and have fun!
66+
67+
To run your tests locally, go to the root directory of the sample code and run the
68+
`mvn clean compile test` command, which AWS CodeBuild also runs through your `buildspec.yml` file.
69+
70+
To test your new code during the release process, modify the existing tests or add tests
71+
to the tests directory. AWS CodeBuild will run the tests during the build stage of your
72+
project pipeline. You can find the test results in the AWS CodeBuild console.
73+
74+
Learn more about Maven's [Standard Directory Layout](https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html).
75+
76+
Learn more about AWS CodeBuild and how it builds and tests your application here:
77+
https://docs.aws.amazon.com/codebuild/latest/userguide/concepts.html
78+
79+
Learn more about AWS CodeStar by reading the user guide. Ask questions or make
80+
suggestions on our forum.
81+
82+
User Guide: http://docs.aws.amazon.com/codestar/latest/userguide/welcome.html
83+
84+
Forum: https://forums.aws.amazon.com/forum.jspa?forumID=248
85+
86+
What Should I Do Before Running My Project in Production?
87+
------------------
88+
89+
AWS recommends you review the security best practices recommended by the framework
90+
author of your selected sample application before running it in production. You
91+
should also regularly review and apply any available patches or associated security
92+
advisories for dependencies used within your application.
93+
94+
Best Practices: https://docs.aws.amazon.com/codestar/latest/userguide/best-practices.html?icmpid=docs_acs_rm_sec

appspec.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 0.0
2+
os: linux
3+
files:
4+
- source: target/ROOT.war
5+
destination: /home/ec2-user/javaapp
6+
hooks:
7+
AfterInstall:
8+
- location: scripts/install_dependencies
9+
timeout: 300
10+
runas: root
11+
- location: scripts/start_server
12+
timeout: 300
13+
runas: root

buildspec.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: 0.2
2+
3+
phases:
4+
install:
5+
commands:
6+
# Upgrade AWS CLI to the latest version
7+
- pip install --upgrade awscli
8+
pre_build:
9+
commands:
10+
- echo Test started on `date`
11+
- mvn clean compile test
12+
build:
13+
commands:
14+
- echo Build started on `date`
15+
- mvn package
16+
post_build:
17+
commands:
18+
- echo Entering post_build phase...
19+
- echo Build completed on `date`
20+
artifacts:
21+
type: zip
22+
files:
23+
- 'appspec.yml'
24+
- 'scripts/start_server'
25+
- 'scripts/install_dependencies'
26+
- 'target/ROOT.war'

pom.xml

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.aws.codestar.projecttemplates</groupId>
5+
<artifactId>HelloWorld</artifactId>
6+
<version>1.0</version>
7+
<packaging>war</packaging>
8+
<name>Sample Spring MVC Application Using AWS CodeStar</name>
9+
10+
<properties>
11+
<junit.platform.version>1.0.1</junit.platform.version>
12+
<junit.jupiter.version>5.0.1</junit.jupiter.version>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework</groupId>
18+
<artifactId>spring-context</artifactId>
19+
<version>4.3.14.RELEASE</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework</groupId>
23+
<artifactId>spring-aop</artifactId>
24+
<version>4.3.14.RELEASE</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework</groupId>
28+
<artifactId>spring-webmvc</artifactId>
29+
<version>4.3.14.RELEASE</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework</groupId>
33+
<artifactId>spring-web</artifactId>
34+
<version>4.3.14.RELEASE</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>javax.servlet</groupId>
38+
<artifactId>javax.servlet-api</artifactId>
39+
<version>3.1.0</version>
40+
<scope>provided</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>commons-fileupload</groupId>
44+
<artifactId>commons-fileupload</artifactId>
45+
<version>1.3.3</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.junit.jupiter</groupId>
49+
<artifactId>junit-jupiter-api</artifactId>
50+
<version>${junit.jupiter.version}</version>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.junit.jupiter</groupId>
55+
<artifactId>junit-jupiter-engine</artifactId>
56+
<version>${junit.jupiter.version}</version>
57+
<scope>test</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.junit.platform</groupId>
61+
<artifactId>junit-platform-launcher</artifactId>
62+
<version>${junit.platform.version}</version>
63+
<scope>test</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.junit.platform</groupId>
67+
<artifactId>junit-platform-runner</artifactId>
68+
<version>${junit.platform.version}</version>
69+
<scope>test</scope>
70+
</dependency>
71+
</dependencies>
72+
73+
<build>
74+
<pluginManagement>
75+
<plugins>
76+
<plugin>
77+
<groupId>org.apache.maven.plugins</groupId>
78+
<artifactId>maven-war-plugin</artifactId>
79+
<version>3.2.0</version>
80+
<configuration>
81+
<warSourceDirectory>src/main/webapp</warSourceDirectory>
82+
<warName>ROOT</warName>
83+
<failOnMissingWebXml>false</failOnMissingWebXml>
84+
</configuration>
85+
</plugin>
86+
<plugin>
87+
<groupId>org.apache.maven.plugins</groupId>
88+
<artifactId>maven-surefire-plugin</artifactId>
89+
<version>2.19.1</version>
90+
<dependencies>
91+
<dependency>
92+
<groupId>org.junit.platform</groupId>
93+
<artifactId>junit-platform-surefire-provider</artifactId>
94+
<version>${junit.platform.version}</version>
95+
</dependency>
96+
<dependency>
97+
<groupId>org.junit.jupiter</groupId>
98+
<artifactId>junit-jupiter-engine</artifactId>
99+
<version>${junit.jupiter.version}</version>
100+
</dependency>
101+
</dependencies>
102+
</plugin>
103+
</plugins>
104+
</pluginManagement>
105+
<finalName>ROOT</finalName>
106+
</build>
107+
</project>

scripts/install_dependencies

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
cd /home/ec2-user/javaapp
3+
wget https://s3.amazonaws.com/dhqs-mirror-iad/apache-tomcat-8.0.39.tar.gz
4+
mkdir /opt/tomcat
5+
tar xvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
6+
cd /opt/tomcat
7+
sed -i 's|port="8080"|port="80"|g' conf/server.xml
8+
rm -rf webapps/ROOT
9+
mv /home/ec2-user/javaapp/ROOT.war webapps/

scripts/start_server

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
cd /opt/tomcat
3+
bin/startup.sh
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.aws.codestar.projecttemplates;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
import com.aws.codestar.projecttemplates.configuration.MvcConfig;
6+
7+
/**
8+
* Utility to initialize the Spring MVC HelloWorld application.
9+
*/
10+
public class HelloWorldAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
11+
12+
@Override
13+
protected Class<?>[] getRootConfigClasses() {
14+
return new Class[] {
15+
MvcConfig.class
16+
};
17+
}
18+
19+
@Override
20+
protected Class<?>[] getServletConfigClasses() {
21+
return null;
22+
}
23+
24+
@Override
25+
protected String[] getServletMappings() {
26+
return new String[] {
27+
"/"
28+
};
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.aws.codestar.projecttemplates.configuration;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.PropertySource;
8+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
9+
10+
import com.aws.codestar.projecttemplates.controller.HelloWorldController;
11+
12+
/**
13+
* Spring configuration for sample application.
14+
*/
15+
@Configuration
16+
@ComponentScan({ "com.aws.codestar.projecttemplates.configuration" })
17+
@PropertySource("classpath:application.properties")
18+
public class ApplicationConfig {
19+
20+
/**
21+
* Retrieved from properties file.
22+
*/
23+
@Value("${HelloWorld.SiteName}")
24+
private String siteName;
25+
26+
@Bean
27+
public HelloWorldController helloWorld() {
28+
return new HelloWorldController(this.siteName);
29+
}
30+
31+
/**
32+
* Required to inject properties using the 'Value' annotation.
33+
*/
34+
@Bean
35+
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
36+
return new PropertySourcesPlaceholderConfigurer();
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.aws.codestar.projecttemplates.configuration;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.Import;
6+
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
7+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
9+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
10+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
11+
12+
13+
/**
14+
* Spring configuration for MVC resolvers.
15+
*/
16+
@EnableWebMvc
17+
@Configuration
18+
@Import({ ApplicationConfig.class })
19+
public class MvcConfig extends WebMvcConfigurerAdapter {
20+
private static final int ONE_YEAR = 12333;
21+
22+
@Override
23+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
24+
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(ONE_YEAR);
25+
}
26+
27+
@Bean
28+
public InternalResourceViewResolver jspViewResolver() {
29+
InternalResourceViewResolver bean = new InternalResourceViewResolver();
30+
bean.setPrefix("/WEB-INF/views/");
31+
bean.setSuffix(".jsp");
32+
return bean;
33+
}
34+
35+
@Bean(name = "multipartResolver")
36+
public CommonsMultipartResolver getMultipartResolver() {
37+
return new CommonsMultipartResolver();
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.aws.codestar.projecttemplates.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestMethod;
6+
import org.springframework.web.servlet.ModelAndView;
7+
8+
/**
9+
* Basic Spring MVC controller that handles all GET requests.
10+
*/
11+
@Controller
12+
@RequestMapping("/")
13+
public class HelloWorldController {
14+
15+
private final String siteName;
16+
17+
public HelloWorldController(final String siteName) {
18+
this.siteName = siteName;
19+
}
20+
21+
@RequestMapping(method = RequestMethod.GET)
22+
public ModelAndView helloWorld() {
23+
ModelAndView mav = new ModelAndView("index");
24+
mav.addObject("siteName", this.siteName);
25+
return mav;
26+
}
27+
28+
}

0 commit comments

Comments
 (0)