Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins): add nx-micronaut-maven plugin #222

Merged
merged 24 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ExecutorContext } from '@nx/devkit';
import { runCommand } from '@jnxplus/common';
import executor from './executor';
import { BuildImageExecutorSchema } from './schema';
jest.mock('@jnxplus/common');

const options: BuildImageExecutorSchema = {
args: '',
};
const context: ExecutorContext = {
root: '/root',
cwd: '/root',
projectName: 'my-app',
targetName: 'build',
workspace: {
version: 2,
projects: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
'my-app': <any>{
root: 'apps/wibble',
sourceRoot: 'apps/wibble',
},
},
npmScope: 'test',
},
isVerbose: false,
};

describe('Build Image Executor', () => {
beforeEach(async () => {
(runCommand as jest.Mock).mockReturnValue({ success: true });
});

xit('can run', async () => {
const output = await executor(options, context);
expect(output.success).toBe(true);
});
});
13 changes: 13 additions & 0 deletions packages/maven/src/executors/build-image/micronaut/executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { runCommand } from '@jnxplus/common';
import { ExecutorContext, logger } from '@nx/devkit';
import { BuildImageExecutorSchema } from './schema';

export default async function runExecutor(
options: BuildImageExecutorSchema,
context: ExecutorContext
) {
logger.info(`Executor ran for Build Image: ${JSON.stringify(options)}`);
return runCommand(
`getExecutable() package -pl :${context.projectName} ${options.args}`
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface BuildImageExecutorSchema {
args: string;
}
14 changes: 14 additions & 0 deletions packages/maven/src/executors/build-image/micronaut/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": 2,
"outputCapture": "pipe",
"$schema": "http://json-schema.org/schema",
"title": "Build Image executor",
"description": "",
"type": "object",
"properties": {
"args": {
"type": "string"
}
},
"required": []
}
42 changes: 42 additions & 0 deletions packages/maven/src/executors/build/executor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ExecutorContext } from '@nx/devkit';
import { runCommand } from '@jnxplus/common';
import executor from './executor';
import { BuildExecutorSchema } from './schema';
jest.mock('@jnxplus/common');

const options: BuildExecutorSchema = {
mvnArgs: '--no-transfer-progress',
mvnBuildCommand: 'compile',
mvnBuildArgs: '-Dquarkus.package.type=uber-jar',
skipClean: true,
};

const context: ExecutorContext = {
root: '/root',
cwd: '/root',
projectName: 'my-app',
targetName: 'build',
workspace: {
version: 2,
projects: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
'my-app': <any>{
root: 'apps/wibble',
sourceRoot: 'apps/wibble',
},
},
npmScope: 'test',
},
isVerbose: false,
};

describe('Build Executor', () => {
beforeEach(async () => {
(runCommand as jest.Mock).mockReturnValue({ success: true });
});

xit('can run', async () => {
const output = await executor(options, context);
expect(output.success).toBe(true);
});
});
53 changes: 53 additions & 0 deletions packages/maven/src/executors/build/executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { getProjectType, isRootProject, runCommand } from '@jnxplus/common';
import { getExecutable, isPomPackaging } from '../../lib/utils';
import { ExecutorContext, logger } from '@nx/devkit';
import { BuildExecutorSchema } from './schema';

export default async function runExecutor(
options: BuildExecutorSchema,
context: ExecutorContext
) {
logger.info(`Executor ran for Build: ${JSON.stringify(options)}`);

if (process.env['NX_MAVEN_CLI_OPTS']) {
if (options.mvnArgs) {
options.mvnArgs += ` ${process.env['NX_MAVEN_CLI_OPTS']}`;
} else {
options.mvnArgs = `${process.env['NX_MAVEN_CLI_OPTS']}`;
}
}

let command = getExecutable();

if (options.mvnArgs) {
command += ` ${options.mvnArgs}`;
}

if (!options.skipClean) {
command += ' clean';
}

if (isPomPackaging(context)) {
command += isRootProject(context) ? ' install -N' : ' install';

return runCommand(`${command} -pl :${context.projectName}`);
}

if (options.mvnBuildCommand) {
command += ` ${options.mvnBuildCommand}`;
} else {
if (getProjectType(context) === 'application') {
command += ' compile';
}

if (getProjectType(context) === 'library') {
command += ' install';
}
}

if (options.mvnBuildArgs) {
command += ` ${options.mvnBuildArgs}`;
}

return runCommand(`${command} -DskipTests=true -pl :${context.projectName}`);
}
8 changes: 8 additions & 0 deletions packages/maven/src/executors/build/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { MvnBuildCommandType } from '@jnxplus/common';

export interface BuildExecutorSchema {
mvnArgs: string;
mvnBuildCommand: MvnBuildCommandType;
mvnBuildArgs: string;
skipClean: boolean;
}
27 changes: 27 additions & 0 deletions packages/maven/src/executors/build/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"version": 2,
"outputCapture": "pipe",
"$schema": "http://json-schema.org/schema",
"title": "Build executor",
"description": "",
"type": "object",
"properties": {
"mvnArgs": {
"type": "string",
"description": "Arguments to pass to the maven cli"
},
"mvnBuildCommand": {
"type": "string",
"description": "Maven build command to use : compile, package or install"
},
"mvnBuildArgs": {
"type": "string",
"description": "Arguments to pass to the maven build command"
},
"skipClean": {
"type": "boolean",
"default": true
}
},
"required": []
}
53 changes: 53 additions & 0 deletions packages/maven/src/executors/serve/micronaut/executor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ExecutorContext } from '@nx/devkit';
import { runCommand } from '@jnxplus/common';
import executor from './executor';
import { ServeExecutorSchema } from './schema';
jest.mock('@jnxplus/common');

const options: ServeExecutorSchema = {
args: 'args',
};
const context: ExecutorContext = {
root: '/root',
cwd: '/root',
projectName: 'my-app',
targetName: 'serve',
workspace: {
version: 2,
projects: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
'my-app': <any>{
root: 'apps/wibble',
sourceRoot: 'apps/wibble',
},
},
npmScope: 'test',
},
isVerbose: false,
};

describe('Serve Executor', () => {
beforeEach(async () => {
(runCommand as jest.Mock).mockReturnValue({ success: true });
});

xit('can run', async () => {
const output = await executor(options, context);

expect(output.success).toBe(true);
expect(runCommand).toHaveBeenCalledWith(
expect.stringMatching(/quarkus:dev -pl :my-app args$/)
);
});

describe('when args option is undefined', () => {
xit('run without extra args', async () => {
const output = await executor({} as ServeExecutorSchema, context);

expect(output.success).toBe(true);
expect(runCommand).toHaveBeenCalledWith(
expect.stringMatching(/quarkus:dev -pl :my-app$/)
);
});
});
});
26 changes: 26 additions & 0 deletions packages/maven/src/executors/serve/micronaut/executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ExecutorContext, logger } from '@nx/devkit';
import { runCommand, waitForever } from '@jnxplus/common';
import { ServeExecutorSchema } from './schema';
import { getExecutable } from '../../../.';

export default async function runExecutor(
options: ServeExecutorSchema,
context: ExecutorContext
) {
logger.info(`Executor ran for serve: ${JSON.stringify(options)}`);

let command = `${getExecutable()} mn:run -pl :${context.projectName}`;

if (options.args) {
command += ` ${options.args}`;
}

const result = runCommand(command);

if (!result.success) {
return { success: false };
}

await waitForever();
return { success: true };
}
3 changes: 3 additions & 0 deletions packages/maven/src/executors/serve/micronaut/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ServeExecutorSchema {
args: string;
}
14 changes: 14 additions & 0 deletions packages/maven/src/executors/serve/micronaut/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": 2,
"outputCapture": "direct-nodejs",
"$schema": "http://json-schema.org/schema",
"title": "Serve executor",
"description": "",
"type": "object",
"properties": {
"args": {
"type": "string"
}
},
"required": []
}
10 changes: 10 additions & 0 deletions packages/nx-micronaut-maven/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
[
"@nx/js/babel",
{
"useBuiltIns": "usage"
}
]
]
}
18 changes: 18 additions & 0 deletions packages/nx-micronaut-maven/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
Empty file.
58 changes: 58 additions & 0 deletions packages/nx-micronaut-maven/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# @jnxplus/nx-micronaut-maven

[![npm version](https://badge.fury.io/js/@jnxplus%2Fnx-micronaut-maven.svg)](https://badge.fury.io/js/@jnxplus%2Fnx-micronaut-maven)

This plugin adds micronaut and Maven multi-module capabilities to Nx workspace.

Here is a quick overview of the plugin, to know more, please visit [the documentation](https://khalilou88.github.io/jnxplus/).

## Supported versions

| @jnxplus/nx-micronaut-maven | Nx | micronaut |
| --------------------------- | ------ | ------------ |
| 0.1.x | 16.x.x | 2.16.6.Final |
| 0.0.x | 15.x.x | 2.16.6.Final |

## Getting Started

### 0. Prerequisites

`@jnxplus/nx-micronaut-maven` requires a Java 17 or higher Runtime Environment and the current Long Term Support (LTS) version of node.js.

### 1. Install the plugin

In the Nx workspace root folder, run this command to install the plugin :

```bash
npm install --save-dev @jnxplus/nx-micronaut-maven
```

### 2. Add micronaut and Maven wrapper support

The following command adds micronaut and Maven support (Maven wrapper and config files) to the workspace. This only needs to be performed once per workspace.

```bash
nx generate @jnxplus/nx-micronaut-maven:init
```

### 3. Usage

| Action | Command |
| ------------------------------------ | --------------------------------------------------------------------- |
| Generate an application | `nx generate @jnxplus/nx-micronaut-maven:application my-app-name` |
| Generate a library | `nx generate @jnxplus/nx-micronaut-maven:library my-lib-name` |
| Build a project | `nx build my-project-name` |
| Build an image | `nx build-image my-app-name` |
| Serve an application | `nx serve my-app-name` |
| Test a project | `nx test my-project-name` |
| Integration Test an application | `nx integration-test my-app-name` |
| Lint a project | `nx lint my-project-name` |
| Format a java project | `nx format --projects my-project-name` |
| Format a kotlin project | `nx ktformat my-project-name` |
| Run a custom task | `nx run-task my-project-name --task="clean install -DskipTests=true"` |
| Publish a project | `nx publish my-project-name` |
| Visualize project's dependency graph | `nx dep-graph` |

## License

MIT © 2023-2023 Khalil LAGRIDA
Loading