Skip to content

Commit

Permalink
fix(nx-spring-boot): fix library projects should not be executable
Browse files Browse the repository at this point in the history
Closes #113
  • Loading branch information
tinesoft committed Jul 3, 2022
1 parent 8c13087 commit b118a4e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 17 deletions.
69 changes: 68 additions & 1 deletion packages/nx-spring-boot/src/generators/project/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jest.mock('node-fetch');
import fetch from 'node-fetch';
const { Response } = jest.requireActual('node-fetch');

import { NX_SPRING_BOOT_PKG } from '@nxrocks/common';
import { hasMavenPlugin, NX_SPRING_BOOT_PKG, stripIndent } from '@nxrocks/common';
import { mockZipEntries, syncToAsyncIterable } from '@nxrocks/common/testing';

const POM_XML = `<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -193,5 +193,72 @@ describe('project generator', () => {
expect(nxJson.plugins).toEqual([NX_SPRING_BOOT_PKG]);

});


it.each`
projectType | expectedAction
${'application'} | ${'keep as-is'}
${'library'} | ${'remove'}
`(`should $expectedAction the Spring Boot Maven plugin in pom.xml when generating a '$projectType'`, async ({ projectType }) => {

const opts: ProjectGeneratorOptions = { ...options, buildSystem: 'maven-project', projectType};

const zipFiles = [{ filePath: 'pom.xml', fileContent: POM_XML }, 'mvnw', 'README.md',];
const starterZip = mockZipEntries(zipFiles);
// mock the zip content returned by the real call to Spring Initializer
jest.spyOn(mockedResponse.body, 'pipe').mockReturnValue(syncToAsyncIterable(starterZip));

await projectGenerator(tree, opts);

const expectedResult = projectType === 'application';
expect(hasMavenPlugin(tree, `./${projectType === 'application' ? 'apps':'libs'}/${options.name}`, 'org.springframework.boot', 'spring-boot-maven-plugin')).toEqual(expectedResult);
});


it.each`
projectType | expectedAction
${'application'} | ${'keep as-is'}
${'library'} | ${'disable'}
`(`should $expectedAction the Spring Boot Gradle plugin in build.gradle when generating a '$projectType'`, async ({ projectType }) => {

const opts: ProjectGeneratorOptions = { ...options, buildSystem: 'gradle-project', projectType};

const zipFiles = [{ filePath: 'build.gradle', fileContent: BUILD_GRADLE }, 'gradlew', 'README.md',];
const starterZip = mockZipEntries(zipFiles);
// mock the zip content returned by the real call to Spring Initializer
jest.spyOn(mockedResponse.body, 'pipe').mockReturnValue(syncToAsyncIterable(starterZip));

await projectGenerator(tree, opts);

const buildGradle = tree.read( `./${projectType === 'application' ? 'apps':'libs'}/${options.name}/build.gradle`, 'utf-8');

const bootJarDisabledTask =
stripIndent`
// spring boot library projects don't need an executable jar, so we disable it
bootJar {
enabled = false
}
`;
const jarEnabledTask =
stripIndent`
jar {
enabled = true
}
`;

if(projectType === 'application') {
expect(buildGradle).not.toContain(bootJarDisabledTask);
expect(buildGradle).not.toContain(jarEnabledTask);
}
else {
expect(buildGradle).toContain(bootJarDisabledTask);
expect(buildGradle).toContain(jarEnabledTask);
}

});


});

Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ export function disableBootJarTask(tree: Tree, options: NormalizedSchema) {

const bootJarDisabledTask =
stripIndent`
// spring boot library projects don't need an executable jar, so we disable it
bootJar {
enabled = false
}
`;

const jarEnabledTask =
stripIndent`
jar {
enabled = true
}
Expand All @@ -26,7 +29,7 @@ export function disableBootJarTask(tree: Tree, options: NormalizedSchema) {
const buildGradlePath = `${options.projectRoot}/build.gradle${ext}`;
let content = tree.read(buildGradlePath).toString();

content += bootJarDisabledTask + '\n' + jarEnabledTask;
content += '\n' + bootJarDisabledTask + '\n' + jarEnabledTask;
tree.write(buildGradlePath, content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,14 @@ import {
logger,
Tree
} from '@nrwl/devkit';
import { removeMavenPlugin } from '@nxrocks/common';
import { NormalizedSchema } from '../schema';

export function removeBootMavenPlugin(tree: Tree, options: NormalizedSchema) {
if (options.projectType === 'library' && options.buildSystem === 'maven-project') {
logger.debug(`Removing 'spring-boot' maven plugin on a library project...`);

const mvnPlugin = `
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
`;
const pomXmlPath = `${options.projectRoot}/pom.xml`;
let content = tree.read(pomXmlPath).toString();

content = content.replace(mvnPlugin, '');
tree.write(pomXmlPath, content);
return removeMavenPlugin(tree, options.projectRoot, 'org.springframework.boot', 'spring-boot-maven-plugin');
}
return false;
}

0 comments on commit b118a4e

Please sign in to comment.