-
Notifications
You must be signed in to change notification settings - Fork 522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate dockerfile for java app #235
Merged
chrisdias
merged 2 commits into
microsoft:master
from
testforstephen:jinbo_javadockerfile
Mar 14, 2018
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import vscode = require('vscode'); | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import * as pomParser from 'pom-parser'; | ||
import * as gradleParser from 'gradle-to-js/lib/parser'; | ||
import { promptForPort, quickPickPlatform } from './config-utils'; | ||
import { reporter } from '../telemetry/telemetry'; | ||
|
||
function genDockerFile(serviceName: string, platform: string, port: string, { cmd, author, version }: PackageJson): string { | ||
function genDockerFile(serviceName: string, platform: string, port: string, { cmd, author, version, artifactName }: PackageJson): string { | ||
switch (platform.toLowerCase()) { | ||
case 'node.js': | ||
|
||
|
@@ -80,6 +82,20 @@ CMD ["python3", "-m", "${serviceName}"] | |
# Using miniconda (make sure to replace 'myenv' w/ your environment name): | ||
#RUN conda env create -f environment.yml | ||
#CMD /bin/bash -c "source activate myenv && python3 -m ${serviceName}" | ||
`; | ||
|
||
case 'java': | ||
const artifact = artifactName ? artifactName : `${serviceName}.jar`; | ||
return ` | ||
FROM openjdk:8-jdk-alpine | ||
VOLUME /tmp | ||
ARG JAVA_OPTS | ||
ENV JAVA_OPTS=$JAVA_OPTS | ||
ADD ${artifact} ${serviceName}.jar | ||
EXPOSE ${port} | ||
ENTRYPOINT exec java $JAVA_OPTS -jar ${serviceName}.jar | ||
# For Spring-Boot project, use the entrypoint below to reduce Tomcat startup time. | ||
#ENTRYPOINT exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar ${serviceName}.jar | ||
`; | ||
|
||
default: | ||
|
@@ -137,6 +153,15 @@ build: . | |
ports: | ||
- ${port}:${port}`; | ||
|
||
case 'java': | ||
return `version: '2.1' | ||
|
||
services: | ||
${serviceName}: | ||
image: ${serviceName} | ||
build: . | ||
ports: | ||
- ${port}:${port}`; | ||
|
||
default: | ||
return `version: '2.1' | ||
|
@@ -221,6 +246,22 @@ services: | |
- ${port}:${port} | ||
`; | ||
|
||
case 'java': | ||
return `version: '2.1' | ||
|
||
services: | ||
${serviceName}: | ||
image: ${serviceName} | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
environment: | ||
JAVA_OPTS: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005,quiet=y | ||
ports: | ||
- ${port}:${port} | ||
- 5005:5005 | ||
`; | ||
|
||
default: | ||
return `version: '2.1' | ||
|
||
|
@@ -255,23 +296,29 @@ interface PackageJson { | |
cmd: string, | ||
fullCommand: string, //full command | ||
author: string, | ||
version: string | ||
version: string, | ||
artifactName: string | ||
} | ||
|
||
async function getPackageJson(folder: vscode.WorkspaceFolder): Promise<vscode.Uri[]> { | ||
return vscode.workspace.findFiles(new vscode.RelativePattern(folder, 'package.json'), null, 1, null); | ||
} | ||
|
||
async function readPackageJson(folder: vscode.WorkspaceFolder): Promise<PackageJson> { | ||
// open package.json and look for main, scripts start | ||
const uris: vscode.Uri[] = await getPackageJson(folder); | ||
var pkg: PackageJson = { | ||
function getDefaultPackageJson(): PackageJson { | ||
return { | ||
npmStart: true, | ||
fullCommand: 'npm start', | ||
cmd: 'npm start', | ||
author: 'author', | ||
version: '0.0.1' | ||
}; //default | ||
version: '0.0.1', | ||
artifactName: '' | ||
}; | ||
} | ||
|
||
async function readPackageJson(folder: vscode.WorkspaceFolder): Promise<PackageJson> { | ||
// open package.json and look for main, scripts start | ||
const uris: vscode.Uri[] = await getPackageJson(folder); | ||
var pkg: PackageJson = getDefaultPackageJson(); //default | ||
|
||
if (uris && uris.length > 0) { | ||
const json = JSON.parse(fs.readFileSync(uris[0].fsPath, 'utf8')); | ||
|
@@ -300,6 +347,49 @@ async function readPackageJson(folder: vscode.WorkspaceFolder): Promise<PackageJ | |
return pkg; | ||
} | ||
|
||
async function readPomAndGradle(folder: vscode.WorkspaceFolder): Promise<PackageJson> { | ||
var pkg: PackageJson = getDefaultPackageJson(); //default | ||
|
||
if (fs.existsSync(path.join(folder.uri.fsPath, 'pom.xml'))) { | ||
const json = await new Promise<any>((resolve, reject) => { | ||
pomParser.parse({ | ||
filePath: path.join(folder.uri.fsPath, 'pom.xml') | ||
}, (error, response) => { | ||
if (error) { | ||
reject(`Failed to parse pom.xml: ${error}`); | ||
return; | ||
} | ||
resolve(response.pomObject); | ||
}); | ||
}); | ||
|
||
if (json.project.version) { | ||
pkg.version = json.project.version; | ||
} | ||
|
||
if (json.project.artifactid) { | ||
pkg.artifactName = `target/${json.project.artifactid}-${pkg.version}.jar`; | ||
} | ||
} else if (fs.existsSync(path.join(folder.uri.fsPath, 'build.gradle'))) { | ||
const json = await gradleParser.parseFile(path.join(folder.uri.fsPath, 'build.gradle')); | ||
|
||
if (json.jar && json.jar.version) { | ||
pkg.version =json.jar.version; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add space after '=' |
||
} else if (json.version) { | ||
pkg.version = json.version; | ||
} | ||
|
||
if (json.jar && json.jar.archiveName) { | ||
pkg.artifactName = `build/libs/${json.jar.archiveName}`; | ||
} else { | ||
const baseName = json.jar && json.jar.baseName ? json.jar.baseName : json.archivesBaseName || folder.name; | ||
pkg.artifactName = `build/libs/${baseName}-${pkg.version}.jar`; | ||
} | ||
} | ||
|
||
return pkg; | ||
} | ||
|
||
const DOCKER_FILE_TYPES = { | ||
'docker-compose.yml': genDockerCompose, | ||
'docker-compose.debug.yml': genDockerComposeDebug, | ||
|
@@ -342,7 +432,12 @@ export async function configure(): Promise<void> { | |
if (!port) return; | ||
|
||
const serviceName = path.basename(folder.uri.fsPath).toLowerCase(); | ||
const pkg = await readPackageJson(folder); | ||
let pkg: PackageJson = getDefaultPackageJson(); | ||
if (platformType.toLowerCase() === 'java') { | ||
pkg = await readPomAndGradle(folder); | ||
} else { | ||
pkg = await readPackageJson(folder); | ||
} | ||
|
||
await Promise.all(Object.keys(DOCKER_FILE_TYPES).map((fileName) => { | ||
return createWorkspaceFileIfNotExists(fileName, DOCKER_FILE_TYPES[fileName]); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readPomOrGradle ?