Skip to content

Commit

Permalink
Support Java Azure Functions (#110)
Browse files Browse the repository at this point in the history

Co-authored-by: Sabrina Juarez Garcia <[email protected]>
  • Loading branch information
sjuarezgx and Sabrina Juarez Garcia authored Dec 15, 2024
1 parent bafb91e commit d7c4f1a
Show file tree
Hide file tree
Showing 7 changed files with 375 additions and 291 deletions.
13 changes: 13 additions & 0 deletions src/AzureFunctions/Definition.target
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,20 @@
<Languages>
<!-- .Net Core -->
<Language Id="41" ApplicationServer="Kestrel"/>
<!-- Java -->
<Language Id="12" ApplicationServer="Generic Servlet 6.0"/>
</Languages>
<Generators>
<Generator>
<Id>12</Id>
<SupportFiles>
<File>services.deployment.azure-serverless.gradle</File>
</SupportFiles>
<Data>
<ApplicationServer>Generic Servlet 6.0</ApplicationServer>
</Data>
</Generator>
</Generators>
<DataSources>
<DataSource Id="12" /> <!-- Microsoft SQL Server -->
<DataSource Id="18" /> <!-- MySQL -->
Expand Down
7 changes: 7 additions & 0 deletions src/AzureFunctions/Templates/Java/host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
322 changes: 40 additions & 282 deletions src/AzureFunctions/createpackage.msbuild

Large diffs are not rendered by default.

42 changes: 33 additions & 9 deletions src/AzureFunctions/deploy.msbuild
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,58 @@
<Error Text= "Error publishing to Azure Functions: $(OUTPUT_MESSAGE)" Condition = "'$(OUTPUT_MESSAGE)' != 'ServiceError: Unknown error' AND '$(AZ_EXIT_CODE)' == '1'" />
</Target>

<Target Name="InitializeMappingVars" Inputs="@(SelectedObject)" Outputs="%(SelectedObject.Identity)" >
<PropertyGroup>
<EntryPointClassName Condition="'$(GENERATOR)' == 'Java'">$(Namespace).%(SelectedObject.QualifiedName)</EntryPointClassName>
<EntryPointClassName Condition = "'$(GENERATOR)' == '.NET Core' or '$(GENERATOR)' == '.NET'">%(SelectedObject.QualifiedName)</EntryPointClassName>
<EntryPointClassName>$(EntryPointClassName.ToLower())</EntryPointClassName>
<UpperFunctionName>$(AZURE_FUNCTIONS_FUNCTION_NAME.ToUpperInvariant())</UpperFunctionName>
<EnvVarName>GX_AZURE_$(UpperFunctionName)_CLASS</EnvVarName>
</PropertyGroup>
</Target>

<Target Name="Deploy" DependsOnTargets="ValidateDeployment;CreateCloudPackage;AzLogin;AzPublish">
<Target Name="Deploy" DependsOnTargets="ValidateDeployment;CreateCloudPackage;AzLogin;AzPublish;InitializeMappingVars">
<Message Text="Deploying to Azure functions finished successfully." Condition="'$(AZ_EXIT_CODE)' == '0' Or '$(OUTPUT_MESSAGE)' == 'ServiceError: Unknown error'" Importance="high"/>

<PropertyGroup Condition="'$(AZ_EXIT_CODE)' == '0' Or '$(OUTPUT_MESSAGE)' == 'ServiceError: Unknown error'" >
<SuccessfullOperation>true</SuccessfullOperation>
</PropertyGroup>

<!-- Define App Settings for CosmosDB -->
<!-- Define App Settings -->
<Message Text="Pushing global app settings ..." Importance="high"/>

<!-- Azure mappings :map function to GX object -->

<Exec Command="az functionapp config appsettings set --name $(AZURE_FUNCTIONS_FUNCTION_APP) --resource-group $(AZURE_FUNCTIONS_RESOURCE_GROUP) --settings &quot;$(EnvVarName)=$(EntryPointClassName)&quot;" EchoOff="true" ContinueOnError="true" Condition="'$(EntryPointClassName)' != ' ' AND '$(EnvVarName)' != '' AND '$(SuccessfullOperation)' == 'true'">
<Output TaskParameter="ExitCode" PropertyName="PUSHMAPPING_EXIT_CODE"/>
</Exec>

<Message Text="Definition of $(EnvVarName) setting was successfull." Importance="high" Condition = "'$(SuccessfullOperation)' == 'true' AND '$(PUSHMAPPING_EXIT_CODE)' == '0'"/>

<!-- CosmosDB -->
<CallTarget Targets="DeployCosmosDB" Condition = "'$(AZURE_FUNCTIONS_TRIGGER_TYPE)' == 'cosmosdb'"/>

<!-- Define App Settings for BlobStorage -->
<!-- BlobStorage -->
<CallTarget Targets="DeployBlobStorage" Condition = "'$(AZURE_FUNCTIONS_TRIGGER_TYPE)' == 'blobstorage'"/>

<!-- Others -->
<!-- Timer -->
<Exec Command="az functionapp config appsettings set --name $(AZURE_FUNCTIONS_FUNCTION_APP) --resource-group $(AZURE_FUNCTIONS_RESOURCE_GROUP) --settings &quot;$(AZURE_FUNCTIONS_SCHEDULE_TIME_APPSETTINGNAME)=$(AZURE_FUNCTIONS_SCHEDULE_TIME_APPSETTINGVALUE)&quot;" EchoOff="true" ContinueOnError="true" Condition="'$(AZURE_FUNCTIONS_TRIGGER_TYPE)' == 'Timer' AND '$(AZURE_FUNCTIONS_SCHEDULE_TIME_FORMAT)'=='appsettingvaluepair' AND '$(AZURE_FUNCTIONS_SCHEDULE_TIME_APPSETTINGNAME)' != '' AND '$(AZURE_FUNCTIONS_SCHEDULE_TIME_APPSETTINGVALUE)' != '' AND '$(SuccessfullOperation)' == 'true'">
<Output TaskParameter="ExitCode" PropertyName="PUSHSETTINGS_EXIT_CODE"/>
<Output TaskParameter="ExitCode" PropertyName="PUSHTIMERSETTINGS_EXIT_CODE"/>
</Exec>

<!-- Queue -->
<Exec Command="az functionapp config appsettings set --name $(AZURE_FUNCTIONS_FUNCTION_APP) --resource-group $(AZURE_FUNCTIONS_RESOURCE_GROUP) --settings &quot;$(AZURE_FUNCTIONS_QUEUE_CONNECTION)=$(AZURE_FUNCTIONS_QUEUE_CONNECTION_APPSETTINGVALUE)&quot;" EchoOff="true" ContinueOnError="true" Condition="'$(AZURE_FUNCTIONS_TRIGGER_TYPE)' == 'Queue' AND '$(AZURE_FUNCTIONS_QUEUE_CONNECTION)' != '' AND '$(AZURE_FUNCTIONS_QUEUE_CONNECTION_APPSETTINGVALUE)' != '' AND '$(SuccessfullOperation)' == 'true'">
<Output TaskParameter="ExitCode" PropertyName="PUSHSETTINGS_EXIT_CODE"/>
<Output TaskParameter="ExitCode" PropertyName="PUSHQUEUESETTINGS_EXIT_CODE"/>
</Exec>

<Exec Command="az functionapp config appsettings set --name $(AZURE_FUNCTIONS_FUNCTION_APP) --resource-group $(AZURE_FUNCTIONS_RESOURCE_GROUP) --settings &quot;$(AZURE_FUNCTIONS_SERVICEBUS_CONNECTION)=$(AZURE_FUNCTIONS_SERVICEBUS_CONNECTION_APPSETTINGVALUE)&quot;" EchoOff="true" ContinueOnError="true" Condition="'$(AZURE_FUNCTIONS_TRIGGER_TYPE)' == 'servicebus' AND '$(AZURE_FUNCTIONS_SERVICEBUS_CONNECTION)' != '' AND '$(AZURE_FUNCTIONS_SERVICEBUS_CONNECTION_APPSETTINGVALUE)' != '' AND '$(SuccessfullOperation)' == 'true'">
<Output TaskParameter="ExitCode" PropertyName="PUSHSETTINGS_EXIT_CODE"/>
<Exec Command="az functionapp config appsettings set --name $(AZURE_FUNCTIONS_FUNCTION_APP) --resource-group $(AZURE_FUNCTIONS_RESOURCE_GROUP) --settings &quot;queue_name=$(AZURE_FUNCTIONS_QUEUE_NAME)&quot;" EchoOff="true" ContinueOnError="true" Condition="'$(AZURE_FUNCTIONS_TRIGGER_TYPE)' == 'Queue' AND '$(AZURE_FUNCTIONS_QUEUE_NAME)' != '' AND '$(GENERATOR)' == 'Java' AND '$(SuccessfullOperation)' == 'true'">
<Output TaskParameter="ExitCode" PropertyName="PUSHQUEUEJAVASETTINGS_EXIT_CODE"/>
</Exec>

<Message Text="Pushing global app settings finished." Importance="high" Condition = "'$(SuccessfullOperation)' == 'true' AND '$(PUSHSETTINGS_EXIT_CODE)' == '0'"/>
<!-- Service Bus -->
<Exec Command="az functionapp config appsettings set --name $(AZURE_FUNCTIONS_FUNCTION_APP) --resource-group $(AZURE_FUNCTIONS_RESOURCE_GROUP) --settings &quot;$(AZURE_FUNCTIONS_SERVICEBUS_CONNECTION)=$(AZURE_FUNCTIONS_SERVICEBUS_CONNECTION_APPSETTINGVALUE)&quot;" EchoOff="true" ContinueOnError="true" Condition="'$(AZURE_FUNCTIONS_TRIGGER_TYPE)' == 'servicebus' AND '$(AZURE_FUNCTIONS_SERVICEBUS_CONNECTION)' != '' AND '$(AZURE_FUNCTIONS_SERVICEBUS_CONNECTION_APPSETTINGVALUE)' != '' AND '$(SuccessfullOperation)' == 'true'">
<Output TaskParameter="ExitCode" PropertyName="PUSHSBSETTINGS_EXIT_CODE"/>
</Exec>

<!-- Define App Settings for GAM connection -->

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependencies {
genexusConfiguration ("com.genexus:gxazureserverless:${genexusVersion}") {
exclude group: 'com.genexus', module: 'gxclassR'
exclude group: 'com.genexus', module: 'gxwrapperjavax'
exclude group: 'com.genexus', module: 'gxwrappercommon'
}
}
89 changes: 89 additions & 0 deletions src/Common/Azure/azurefunctions-create-Javapackage.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="CreateJavaPackage" ToolsVersion="4.0">
<PropertyGroup>
<ApplicationType Condition="'$(AZURE_FUNCTIONS_TRIGGER_TYPE)' == 'http'">Web</ApplicationType>
<ApplicationType Condition="'$(AZURE_FUNCTIONS_TRIGGER_TYPE)' != 'http'">Function</ApplicationType>
</PropertyGroup>
<Target Name="CreateJavaPackage" DependsOnTargets="PreValidate;ValidatePackaging;CreateAzureJavaPackage;ValidateResultsPackage;" Condition="'$(GENERATOR)' == 'Java'" />

<Target Name="PreValidate" Condition="'$(GENERATOR)' == 'Java'">
<Message Text="Start Create Package for $(ApplicationType)" Importance="high"/>
<PropertyGroup>
<!-- ApplicationType Web or Function -->
<AzureTemporalDir>$(DeployFullPath)/azuredeploy</AzureTemporalDir>
<ClassesDir Condition="'$(ApplicationType)' == 'Web'">$(DeployFullPath)/WEB-INF/classes</ClassesDir>
<ClassesDir Condition="'$(ApplicationType)' != 'Web'">$(DeployFullPath)/classes</ClassesDir>
</PropertyGroup>

<Error Text="'ApplicationType' is invalid: '$(ApplicationType)'. 'Web' or 'Function' must be specified." Condition="'$(ApplicationType)' == ''"/>
<Error Text="Class directory is invalid: $(ClassesDir)" Condition="!Exists('$(ClassesDir)')"/>

</Target>

<Target Name="CreateAzureJavaPackage" DependsOnTargets="PrepareAzurePackageJava;JavaPackageZipDeploy" Condition="'$(GENERATOR)' == 'Java'"/>

<Target Name="PrepareAzurePackageJava" DependsOnTargets="GenerateConfigs" Condition="'$(GENERATOR)' == 'Java'">
<Message Text="Adding required files to the package...(this may take a while)" Importance="high"/>
<ItemGroup>

<MetadataFiles Include="$(DeployFullPath)/Metadata/**/*"></MetadataFiles>

<ConfigFiles Include="$(DeployFullPath)/**/*.txt"/>
<ConfigFiles Include="$(DeployFullPath)/**/*.key"/>
<ConfigFiles Include="$(DeployFullPath)/**/*.config"/>
<ConfigFiles Include="$(DeployFullPath)/**/*.cfg"/>
<ConfigFiles Include="$(DeployFullPath)/**/log4j2.xml"/>
<ConfigFiles Include="$(DeployFullPath)/**/*.services"/>
<ConfigFiles Include="$(DeployFullPath)/**/*.gam"/>
<ConfigFiles Include="$(DeployFullPath)/**/*.ini"/>
<ConfigFiles Include="$(ClassesDir)/*.*"/>

<GAMConfigFiles Include="$(DeployFullPath)/**/*.gam"/>
</ItemGroup>

<CreateItem Include="$(DeployFullPath)/**/*.jar;">
<Output TaskParameter="Include" ItemName="LibFiles"/>
</CreateItem>

<Copy SourceFiles="@(LibFiles)"
DestinationFiles="@(LibFiles -> '$(AzureTemporalDir)/lib/%(Filename)%(Extension)')"/>

<Copy SourceFiles="@(ConfigFiles)"
DestinationFiles="@(ConfigFiles -> '$(AzureTemporalDir)/%(Filename)%(Extension)')"/>

<Copy SourceFiles="@(GAMConfigFiles)"
DestinationFiles="@(GAMConfigFiles -> '$(AzureTemporalDir)/WEB-INF/%(Filename)%(Extension)')"/>

<Copy SourceFiles="@(MetadataFiles)"
DestinationFolder="$(AzureTemporalDir)/Metadata/%(RecursiveDir)"/>

<!--Copy application JAR to lib directory if it's not WEB -->
<Copy SourceFiles="$(DeployFileFullPath)/$(ProjectName).jar" DestinationFolder="$(AzureTemporalDir)/lib" Condition ="'$(ApplicationType)' == 'Function' AND Exists('$(DeployFileFullPath)/$(ProjectName).jar')" />

<!--Copy Azure Functions handler to the root directory -->
<ItemGroup>
<HandlerJarFile Include="$(DeployFullPath)/gxazureserverless*.jar" />
</ItemGroup>

<Copy SourceFiles="@(HandlerJarFile)" DestinationFiles="$(AzureTemporalDir)/gx-azure-serverless.jar" />

<!--Create a directory for the Azure Function -->
<Copy SourceFiles="$(DeployFullPath)/function.json" DestinationFolder="$(AzureTemporalDir)/$(AZURE_FUNCTIONS_FUNCTION_NAME)" Condition ="Exists('$(DeployFullPath)/function.json') AND '$(AZURE_FUNCTIONS_FUNCTION_NAME)' != ''" />

<!-- Copy host.json file -->
<Copy SourceFiles="$(GX_PROGRAM_DIR)/DeploymentTargets/AzureFunctions/Templates/Java/host.json" DestinationFolder="$(AzureTemporalDir)" />
</Target>

<Target Name="ValidateResultsPackage" Condition="'$(GENERATOR)' == 'Java'">
<Error Text="Azure Functions Package could not be created" Condition="!Exists('$(DeployFileFullPath)/$(ProjectName)_$(AZURE_FUNCTIONS_FUNCTION_NAME).zip')" Importance="high"/>
<Message Text="Application successfully Packaged to be uploaded to Azure : $(DeployFileFullPath)/$(ProjectName)_$(AZURE_FUNCTIONS_FUNCTION_NAME).zip" Condition="Exists('$(DeployFileFullPath)/$(ProjectName)_$(AZURE_FUNCTIONS_FUNCTION_NAME).zip')" Importance="high"/>
</Target>


<Target Name="JavaPackageZipDeploy" Condition="'$(GENERATOR)' == 'Java'">
<CreateItem Include="$(AzureTemporalDir)/**/*.*" >
<Output ItemName="ZipFiles" TaskParameter="Include"/>
</CreateItem>
<Zip ZipFileName="$(DeployFileFullPath)/$(ProjectName)_$(AZURE_FUNCTIONS_FUNCTION_NAME).zip" WorkingDirectory="$(AzureTemporalDir)" Files="@(ZipFiles)" />

</Target>
</Project>
Loading

0 comments on commit d7c4f1a

Please sign in to comment.