-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from lenala/jar_deployment
Spring Boot Jar deployment
- Loading branch information
Showing
26 changed files
with
1,158 additions
and
14 deletions.
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
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 |
---|---|---|
|
@@ -11,5 +11,6 @@ public enum DeploymentType { | |
FTP, | ||
WAR, | ||
ZIP, | ||
JAR, | ||
UNKNOWN; | ||
} |
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
110 changes: 110 additions & 0 deletions
110
...ugin/src/main/java/com/microsoft/azure/gradle/webapp/handlers/JarArtifactHandlerImpl.java
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for | ||
* license information. | ||
*/ | ||
package com.microsoft.azure.gradle.webapp.handlers; | ||
|
||
import com.google.common.io.Files; | ||
import com.microsoft.azure.gradle.webapp.DeployTask; | ||
import com.microsoft.azure.gradle.webapp.configuration.AppServiceType; | ||
import com.microsoft.azure.gradle.webapp.configuration.DeployTarget; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.gradle.api.GradleException; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Paths; | ||
|
||
/** | ||
* Artifact handler for deploying a JAR, self-contained, Java application (e.g. | ||
* Spring Boot) to Azure App Service through FTP | ||
* | ||
* @since 1.3.0 | ||
*/ | ||
public final class JarArtifactHandlerImpl extends ZipArtifactHandlerImpl { | ||
private static final String FILE_IS_NOT_JAR = "The deployment file is not a jar typed file."; | ||
private static final String FIND_JAR_FILE_FAIL = "Failed to find the jar file: '%s'"; | ||
|
||
private static final String DEFAULT_LINUX_JAR_NAME = "app.jar"; | ||
private static final String JAR_CMD = ":JAR_COMMAND:"; | ||
private static final String FILENAME = ":FILENAME:"; | ||
private static final String DEFAULT_JAR_COMMAND = "-Djava.net.preferIPv4Stack=true " + | ||
"-Dserver.port=%HTTP_PLATFORM_PORT% " + | ||
"-jar "%HOME%\\\\site\\\\wwwroot\\\\:FILENAME:""; | ||
private static final String GENERATE_WEB_CONFIG_FAIL = "Failed to generate web.config file for JAR deployment."; | ||
private static final String READ_WEB_CONFIG_TEMPLATE_FAIL = "Failed to read the content of web.config.template."; | ||
private static final String GENERATING_WEB_CONFIG = "Generating web.config for Web App on Windows."; | ||
|
||
protected JarArtifactHandlerImpl(final DeployTask task) { | ||
super(task); | ||
} | ||
|
||
@Override | ||
public void publish(DeployTarget deployTarget) throws IOException { | ||
final File jar = getJarFile(); | ||
assureJarFileExisted(jar); | ||
|
||
prepareDeploymentFiles(jar); | ||
|
||
super.publish(deployTarget); | ||
} | ||
|
||
protected void prepareDeploymentFiles(File jar) throws IOException { | ||
final File parent = new File(getDeploymentStagingDirectoryPath()); | ||
parent.mkdirs(); | ||
|
||
if (AppServiceType.LINUX.equals(task.getAzureWebAppExtension().getAppService().getType())) { | ||
Files.copy(jar, new File(parent, DEFAULT_LINUX_JAR_NAME)); | ||
} else { | ||
Files.copy(jar, new File(parent, jar.getName())); | ||
generateWebConfigFile(jar.getName()); | ||
} | ||
} | ||
|
||
protected void generateWebConfigFile(String jarFileName) throws IOException { | ||
task.getLogger().quiet(GENERATING_WEB_CONFIG); | ||
final String templateContent; | ||
try (final InputStream is = getClass().getResourceAsStream("web.config.template")) { | ||
templateContent = IOUtils.toString(is, "UTF-8"); | ||
} catch (IOException e) { | ||
task.getLogger().quiet(READ_WEB_CONFIG_TEMPLATE_FAIL); | ||
throw e; | ||
} | ||
|
||
final String webConfigFile = templateContent | ||
.replaceAll(JAR_CMD, DEFAULT_JAR_COMMAND.replaceAll(FILENAME, jarFileName)); | ||
|
||
final File webConfig = new File(getDeploymentStagingDirectoryPath(), "web.config"); | ||
webConfig.createNewFile(); | ||
|
||
try (final FileOutputStream fos = new FileOutputStream(webConfig)) { | ||
IOUtils.write(webConfigFile, fos, "UTF-8"); | ||
} catch (Exception e) { | ||
task.getLogger().quiet(GENERATE_WEB_CONFIG_FAIL); | ||
throw e; | ||
} | ||
} | ||
|
||
protected File getJarFile() { | ||
String jarFile = task.getAzureWebAppExtension().getDeployment().getJarFile(); | ||
if (StringUtils.isEmpty(jarFile)) { | ||
jarFile = task.getProject().getTasks().getByPath("bootJar").getOutputs().getFiles().getAsPath(); | ||
} | ||
return new File(jarFile); | ||
} | ||
|
||
protected void assureJarFileExisted(File jar) throws GradleException { | ||
if (!Files.getFileExtension(jar.getName()).equalsIgnoreCase("jar")) { | ||
throw new GradleException(FILE_IS_NOT_JAR); | ||
} | ||
|
||
if (!jar.exists() || !jar.isFile()) { | ||
throw new GradleException(String.format(FIND_JAR_FILE_FAIL, jar.getAbsolutePath())); | ||
} | ||
} | ||
} | ||
|
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
10 changes: 10 additions & 0 deletions
10
...-plugin/src/main/resources/com/microsoft/azure/gradle/webapp/handlers/web.config.template
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
<system.webServer> | ||
<handlers> | ||
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/> | ||
</handlers> | ||
<httpPlatform processPath="%JAVA_HOME%\bin\java.exe" arguments=":JAR_COMMAND:"> | ||
</httpPlatform> | ||
</system.webServer> | ||
</configuration> |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE |
Oops, something went wrong.