diff --git a/packages/common/src/lib/core/jvm/gradle-utils.spec.ts b/packages/common/src/lib/core/jvm/gradle-utils.spec.ts index 2be32674..21b4817d 100644 --- a/packages/common/src/lib/core/jvm/gradle-utils.spec.ts +++ b/packages/common/src/lib/core/jvm/gradle-utils.spec.ts @@ -194,6 +194,21 @@ describe('gradle-utils', () => { `); }); + it('should still add plugin to build.gradle file when version is not specified', () => { + tree.write('./build.gradle', ''); + const added = addGradlePlugin(tree, '.', 'java', 'java-library'); + expect(added).toBe(true); + + const buildGradle = tree.read('./build.gradle', 'utf-8'); + console.log(buildGradle); + expect(buildGradle).toContain( + stripIndent` + plugins { + id 'java-library' + } + `); + }); + }); describe('applySpotlessGradlePlugin', () => { diff --git a/packages/common/src/lib/core/jvm/gradle-utils.ts b/packages/common/src/lib/core/jvm/gradle-utils.ts index cae8e53b..a73c281c 100644 --- a/packages/common/src/lib/core/jvm/gradle-utils.ts +++ b/packages/common/src/lib/core/jvm/gradle-utils.ts @@ -36,10 +36,14 @@ export function hasGradlePlugin(content: string, pluginId: string, pluginVersion return plugins.some(plugin => plugin.id === pluginId && (!pluginVersion || plugin.version === pluginVersion)); } -export function addGradlePlugin(tree: Tree, rootFolder: string, language: 'java' | 'kotlin' | 'groovy', pluginId: string, pluginVersion: string, withKotlinDSL = language === 'kotlin') { +export function addGradlePlugin(tree: Tree, rootFolder: string, language: 'java' | 'kotlin' | 'groovy', pluginId: string, pluginVersion?: string, withKotlinDSL = language === 'kotlin') { const ext = withKotlinDSL ? '.gradle.kts' : '.gradle'; const buildGradle = tree.read(`${rootFolder}/build${ext}`, 'utf-8'); - const pluginToAdd = withKotlinDSL ? `id("${pluginId}") version "${pluginVersion}"` : `id '${pluginId}' version '${pluginVersion}'`; + let withVersion = ''; + if(pluginVersion) { + withVersion = withKotlinDSL ? ` version "${pluginVersion}"` : ` version '${pluginVersion}'`; + } + const pluginToAdd = withKotlinDSL ? `id("${pluginId}")${withVersion}` : `id '${pluginId}'${withVersion}`; if (!hasGradlePlugin(buildGradle, pluginId)) { if (!GRADLE_PLUGINS_REGEX.test(buildGradle)) {