Gradle: how to copy file but keep the same build invocation? - configuration

Currently my Gradle build produces a fat JAR (via ShadowJar Plugin) under build/distributions via the following build invocation:
gradle clean build shadowJar
I now need that same exact build invocation to copy src/main/resources/myconfig.json to build/distributions as well. I followed the Gradle docs and added the following to my build:
task copyConfig(type: Copy) {
into 'build/distributions'
from('src/main/resources') {
include '**/*.json'
}
}
However running gradle clean build shadowJar doesn't produce a build/distributions/myconfig.json as expected. What can I do to keep the build invocation exactly the same, but to invoke the copyConfig task (also, I'm not even 100% sure that task is error-free)?

You have created a task, but you never execute it. In order for this task to be invoked when executing build or shadowJar, one of these tasks needs to depend on the task you created:
build.dependsOn copyConfig
or
shadowJar.dependsOn copyConfig

Related

How to perform web.config transformation in release pipeline

I have a build pipeline which publishes a build artifact to path $(Build.ArtifactStagingDirectory). I have a release pipeline with 1 stage following the IIS website deployment template. The IIS web App Deploy task is set to use Xml Transformation, and the deployment executes successfully. The website is created in IIS, but the web.config is not transformed correct.
My Stage is called "MyEnvironment", and I have a web.config and a web.MyEnvironment.Config file in the project, which transforms correctly in visual studio transform preview and if I publish from visual studio. If I look at the logs for my release pipeline, I see that the IIS Web App Deploy task executed successfully but there were warnings:
2022-02-09T19:05:07.5762631Z ##[warning]Unable to apply transformation for the given package. Verify the following.
2022-02-09T19:05:07.5773252Z ##[warning]1. Whether the Transformation is already applied for the MSBuild generated package during build. If yes, remove the <DependentUpon> tag for each config in the csproj file and rebuild.
2022-02-09T19:05:07.5775568Z ##[warning]2. Ensure that the config file and transformation files are present in the same folder inside the package.
My understanding was that the xml transformation of an IIS Web App Deploy task would use the name of the stage as the environment when searching for the web.config transform to use. I'm sure that the stage name matches the web..config file, yet it is still not transforming correctly. Is there something I'm missing here?
trigger:
- Development
pool:
vmImage: 'windows-latest'
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
steps:
- task: NuGetToolInstaller#1
- task: NuGetCommand#2
inputs:
restoreSolution: '$(solution)'
- task: VSBuild#1
inputs:
solution: '$(solution)'
# msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)" /p:AutoParameterizationWebConfigConnectionStrings=False'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: VSTest#2
inputs:
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
You will need to turn off the transforms during the build in the yml and in the project file.
In your yml file under the VSBuild#1 task in you msbuildArgs add /p:TransformWebConfigEnabled=false
In your project file find
<Content Include="web.MyEnvironment.config">
<DependentUpon>web.config</DependentUpon>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
And remove the <DependentUpon>web.config</DependentUpon> but make sure <CopyToOutputDirectory>Always</CopyToOutputDirectory> is there, you may need to add it.
When you build, your web.config and web.MyEnvironment.config should both be in the drop artifacts at the base level of your web app as well as in the bin folder, not just in the bin folder. The rest of your settings should be correct. You can reference this question as well.

artifactoryPublish task is looking for default zip file of distribution plugin

I upgraded my enviornment from gradle 1.12 to gradle 2.10 & java7 to java8.After upgrading my environment all of sudden one task of my build.gradle is not working
artifactoryPublish.I am using distrobution plugin to create a custom zip files.Below is the error I am getting
What went wrong:
14:24:05 Execution failed for task ':artifactoryPublish'.
14:24:05 > File 'F:\jenkins03\workspace\DataGenerator\build\distributions\DataGenerator-28_42_00_00.zip' does not exists, and need to be published!
I am not sure why it is trying to look for default zip files
Include the distributions block in the gradle script where you have included the distribution plugin. Below is the sample.
distributions {
main {
baseName = archivesBaseName
contents {
...
...
}
}
}

MSDeploy gulp build package

Is it possible to deploy a Single Page App project build using grunt to IIS using MSDeploy from TeamCity? The project is not any kind of Visual Studio solution and doesn't get built using MSBuild.
My Command parameters which are not working are:
-source:package='%teamcity.build.checkoutDir%\Dist.%build.number%.zip' -dest:auto,computerName="%system.MsDeployServiceUrl%",userName="%system.UserName%",password="%system.Password%",authtype="basic",includeAcls="False"
-verb:sync -setParamFile:"%teamcity.build.checkoutDir%\Dist.%build.number%.zip.SetParameters.xml"
-AllowUntrusted -setParam:"IIS Web Application Name"="%system.WebSiteName%" -verbose
The error I am getting is:
[11:47:31][Step 3/3] Error Code: ERROR_EXCEPTION_WHILE_CREATING_OBJECT
[11:47:31][Step 3/3] More Information: Object of type 'package' and
path 'D:\TeamCity\buildAgent\work\e2b0015b49d87e90\Dist.30.zip' cannot
be created. Learn more at:
http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_EXCEPTION_WHILE_CREATING_OBJECT.
[11:47:31][Step 3/3] Error: The Zip package
'D:\TeamCity\buildAgent\work\e2b0015b49d87e90\Dist.30.zip' could not
be loaded. [11:47:31][Step 3/3] Error: Could not find file
'D:\TeamCity\buildAgent\work\e2b0015b49d87e90\Dist.30.zip'.
[11:47:31][Step 3/3] Error count: 1. [11:47:31][Step 3/3] Process
exited with code -1 [11:47:31][Step 3/3] Step Deploy (Command Line)
failed
My build process is working as I end up with the correct artefacts, I just don't seem to be able to deploy my generated artefacts using MSDeploy
This is a screenshot of my artefacts:
I managed to get this working by changing my parameters to the following:
-source:iisapp='%teamcity.build.checkoutDir%\dist' -dest:iisapp='C:\www\xxxx-website',computerName="%system.MsDeployServiceUrl%",userName="%system.UserName%",password="%system.Password%",authtype="basic",includeAcls="False"
-verb:sync -AllowUntrusted -verbose
And changing my user to an admin user rather than an IIS user. Note use of iisapp - the key was to read the MSDeploy api using msdeploy -help
FYI - a good test is to use the intended command against msdeploy.exe in console and check output errors then push command into teamcity when it's working.
I created a grunt and gulp plugin to do just what you are looking to do. gulp-mswebdeploy-package and grunt-mswebdeploy-package will create a ms webdeploy package from any folder and do not require your build to be running on windows.
https://www.npmjs.com/package/gulp-mswebdeploy-package
https://www.npmjs.com/package/grunt-mswebdeploy-package

How to configure UglifyJS with Gradle?

Are there any ready-to-use Gradle plugins to use for UglifyJs? We are trying to configure Uglify something similar to what has been done here, but the owner of that project seems to have his own private artifactory to which he points to, thereby getting access to UglifyAntTask, which is a github-hosted project not following Gradle/Maven etc. (basically non-managed) JAR. We tried downloading this JAR to our project and tried configuring using the options suggested in gradle page as follows:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar') (or)
compile files('uglifyjs-java-v1.0.jar')
}
Note: The (or) is not there in actual code, I mentioned only to indicate that we tried both options but it was not picking the JAR.
So at a later step, when we gave
ant.taskdef(name: "uglify", classname: "uglify.ant.UglifyTask", classpath: configurations.uglifyjs.asPath)
Gradle throws the following errror:
taskdef class uglify.ant.UglifyTask cannot be found
using the classloader AntClassLoader[]
I am hoping that at least some one must have had the need to include non-managed 3rd party JAR and have figured out how to do this, if so, please point the solution/mistake we have made.
Thanks,
Paddy
Here is how the offical Gradle documentation describe it:
configurations {
uglifyjs
}
dependencies {
uglifyjs files('uglifyjs-java-v1.0.jar')
}
task uglifyjs << {
ant.taskdef(name: 'uglifyjs', classname: 'uglify.ant.UglifyTask', classpath: configurations.uglifyjs.asPath)
ant.uglifyjs( ... UglifyJS Ant Task parameters ... )
}
See http://www.gradle.org/docs/current/userguide/ant.html#N11416
HTH

Mark Gradle build unstable in Jenkins when JUnit tests fail

I have a Gradle build in Jenkins with various JUnit tests that are executed as part of the build. Now when some of the tests fail the complete build is marked as failed - because Gradle says the build failed.
How can I convince Gradle to succeed the build and then Jenkins to mark the build as unstable? With ant this was no problem at all.
Use the ignoreFailures property in the test task.
apply plugin: 'java'
test {
ignoreFailures = true
}
You can use external properties to solve this problem.
if (!ext.has('ignoreTestFailures')) {
ext.ignoreTestFailures = false
}
test {
ignoreFailures = project.ext.ignoreTestFailures
}
In this setup by default failures will fail the build. But if you call Gradle like so: gradle -PignoreTestFailures=true test then the test failures will not fail the build. So you can configure Jenkins to ignore test failures, but to fail the build when a developer runs the tests manually.
You can include this in your main build.gradle to be applied to all projects and all test tasks.
allprojects{
tasks.withType(Test) {
ignoreFailures=true;
}
}
Since just ignoring the failed test could not be used in my case i found out the following.
If you are using a scripted jenkinsfile. It is possible to wrap your test stage in a try-catch statement.
try {
stage('test') {
sh './gradlew test'
}
} catch (e) {
echo "Test FAILED"
}
This will catch the build exception thrown by gradle but it marks the build as unstable.