Menu
AWS CodeBuild
User Guide (API Version 2016-10-06)

Maven in 5 Minutes Sample for AWS CodeBuild

This Maven sample produces as build output a single JAR file named my-app-1.0-SNAPSHOT.jar. This sample is based on the Maven in 5 Minutes topic on the Apache Maven website.

Important

Running this sample may result in charges to your AWS account. These include possible charges for AWS CodeBuild and for AWS resources and actions related to Amazon S3, AWS KMS, and CloudWatch Logs. For more information, see AWS CodeBuild Pricing, Amazon S3 Pricing, AWS Key Management Service Pricing, and Amazon CloudWatch Pricing.

Running the Sample

To run this sample:

  1. Download and install Maven. For more information, see Downloading Apache Maven and Installing Apache Maven on the Apache Maven website.

  2. Switch to an empty directory on your local computer or instance, and then run this Maven command.

    Copy
    mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    If successful, this directory structure and files will be created.

    Copy
    (root directory name) `-- my-app |-- pom.xml `-- src |-- main | `-- java | `-- com | `-- mycompany | `-- app | `-- App.java `-- test `-- java `-- com `-- mycompany `-- app `-- AppTest.java
  3. Create a file with this content. Name the file buildspec.yml, and then add it to the (root directory name)/my-app directory.

    Copy
    version: 0.2 phases: build: commands: - echo Build started on `date` - mvn test post_build: commands: - echo Build completed on `date` - mvn package artifacts: files: - target/my-app-1.0-SNAPSHOT.jar

    When finished, your directory structure and file should look like this.

    Copy
    (root directory name) `-- my-app |-- buildspec.yml |-- pom.xml `-- src |-- main | `-- java | `-- com | `-- mycompany | `-- app | `-- App.java `-- test `-- java `-- com `-- mycompany `-- app ` -- AppTest.java
  4. Upload this contents of the my-app directory to an Amazon S3 input bucket or an AWS CodeCommit, GitHub, or Bitbucket repository.

    Important

    Do not upload (root directory name) or (root directory name)/my-app, just the directories and files inside of (root directory name)/my-app.

    If you are using an Amazon S3 input bucket, be sure to create a ZIP file that contains the directory structure and files, and then upload it to the input bucket. Do not add (root directory name) or (root directory name)/my-app to the ZIP file, just the directories and files inside of (root directory name)/my-app.

  5. Create a build project, run the build, and view related build information by following the steps in Run AWS CodeBuild Directly.

    If you use the AWS CLI to create the build project, the JSON-formatted input to the create-project command might look similar to this. (Replace the placeholders with your own values.)

    Copy
    { "name": "sample-maven-in-5-minutes-project", "source": { "type": "S3", "location": "codebuild-region-ID-account-ID-input-bucket/MavenIn5MinutesSample.zip" }, "artifacts": { "type": "S3", "location": "codebuild-region-ID-account-ID-output-bucket", "packaging": "ZIP", "name": "MavenIn5MinutesOutputArtifact.zip" }, "environment": { "type": "LINUX_CONTAINER", "image": "aws/codebuild/java:openjdk-8", "computeType": "BUILD_GENERAL1_SMALL" }, "serviceRole": "arn:aws:iam::account-ID:role/role-name", "encryptionKey": "arn:aws:kms:region-ID:account-ID:key/key-ID" }
  6. To get the build output artifact, open your Amazon S3 output bucket.

  7. Download the MavenIn5MinutesOutputArtifact.zip file to your local computer or instance, and then extract the contents of the MavenIn5MinutesOutputArtifact.zip file. In the extracted contents, open the target folder to get the my-app-1.0-SNAPSHOT.jar file.

Related Resources