Skip to content

Commit

Permalink
feat(java): projenrc.java (projen#470)
Browse files Browse the repository at this point in the history
If `projenrcJava` is set to `true` for java projects (or `--projenrc-java` switch is provided in `projen new`, the java project will support writing your projenrc file in java (yeah!).

In this case, the projenrc file will be located under `src/test/java/projenrc.java` and `projen synth` will take care to compile it and execute it with a dependency on `com.github.eladb/projen`.

Next steps: 

1. The CLI should auto-detect the `synth` task and defer to it when `pj` is executed without a subcommand. This will allow projects to customize how synth is performed for each language.
2. Currently `projen new` will still generate a `.projenrc.js` file, but this should be moved from the CLI to the library and then `java.Projenrc` should generate the Java equivalent in the right location when the project is bootstrapped. That should be fun.

---
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
Elad Ben-Israel authored Jan 7, 2021
1 parent 0ee35fa commit 9d099f6
Show file tree
Hide file tree
Showing 11 changed files with 854 additions and 1 deletion.
53 changes: 53 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Name|Description
[java.MavenPackaging](#projen-java-mavenpackaging)|Configures a maven project to produce a .jar archive with sources and javadocs.
[java.MavenSample](#projen-java-mavensample)|Java code sample.
[java.Pom](#projen-java-pom)|A Project Object Model or POM is the fundamental unit of work in Maven.
[java.Projenrc](#projen-java-projenrc)|Allows writing projenrc files in java.
[tasks.Task](#projen-tasks-task)|A task that can be performed on the project.
[tasks.TaskRuntime](#projen-tasks-taskruntime)|The runtime component of the tasks engine.
[tasks.Tasks](#projen-tasks-tasks)|Defines project tasks.
Expand Down Expand Up @@ -146,6 +147,7 @@ Name|Description
[java.PluginExecution](#projen-java-pluginexecution)|Plugin execution definition.
[java.PluginOptions](#projen-java-pluginoptions)|Options for Maven plugins.
[java.PomOptions](#projen-java-pomoptions)|Options for `Pom`.
[java.ProjenrcOptions](#projen-java-projenrcoptions)|Options for `Projenrc`.
[tasks.TaskCommonOptions](#projen-tasks-taskcommonoptions)|*No description*
[tasks.TaskOptions](#projen-tasks-taskoptions)|*No description*
[tasks.TaskSpec](#projen-tasks-taskspec)|Specification of a single task.
Expand Down Expand Up @@ -4207,6 +4209,8 @@ new java.JavaProject(options: JavaProjectOptions)
* **junit** (<code>boolean</code>) Include junit tests. __*Default*__: true
* **junitOptions** (<code>[java.JunitOptions](#projen-java-junitoptions)</code>) junit options. __*Default*__: defaults
* **packagingOptions** (<code>[java.MavenPackagingOptions](#projen-java-mavenpackagingoptions)</code>) Packaging options. __*Default*__: defaults
* **projenrcJava** (<code>boolean</code>) Use projenrc in java. __*Default*__: false
* **projenrcJavaOptions** (<code>[java.ProjenrcOptions](#projen-java-projenrcoptions)</code>) Options related to projenrc in java. __*Default*__: default options
* **sample** (<code>boolean</code>) Include sample code and test if the relevant directories don't exist. __*Optional*__
* **sampleJavaPackage** (<code>string</code>) The java package to use for the code sample. __*Default*__: "org.acme"
* **testDeps** (<code>Array<string></code>) List of test dependencies for this project. __*Default*__: []
Expand All @@ -4223,6 +4227,7 @@ Name | Type | Description
**packaging**🔹 | <code>[java.MavenPackaging](#projen-java-mavenpackaging)</code> | Packaging component.
**pom**🔹 | <code>[java.Pom](#projen-java-pom)</code> | API for managing `pom.xml`.
**junit**?🔹 | <code>[java.Junit](#projen-java-junit)</code> | JUnit component.<br/>__*Optional*__
**projenrc**?🔹 | <code>[java.Projenrc](#projen-java-projenrc)</code> | Projenrc component.<br/>__*Optional*__

### Methods

Expand Down Expand Up @@ -4504,6 +4509,37 @@ addTestDependency(spec: string): void



## class Projenrc 🔹 <a id="projen-java-projenrc"></a>

Allows writing projenrc files in java.

This will install `org.projen/projen` as a Maven dependency and will add a
`synth` task which will compile & execute `main()` from
`src/main/java/projenrc.java`.

__Submodule__: java

__Extends__: [Component](#projen-component)

### Initializer




```ts
new java.Projenrc(project: Project, pom: Pom, options?: ProjenrcOptions)
```

* **project** (<code>[Project](#projen-project)</code>) *No description*
* **pom** (<code>[java.Pom](#projen-java-pom)</code>) *No description*
* **options** (<code>[java.ProjenrcOptions](#projen-java-projenrcoptions)</code>) *No description*
* **className** (<code>string</code>) The name of the Java class which contains the `main()` method for projen. __*Default*__: "projenrc"
* **projenVersion** (<code>string</code>) The projen version to use. __*Default*__: current version
* **testScope** (<code>boolean</code>) Defines projenrc under the test scope instead of the main scope, which is reserved to the app. __*Default*__: true




## class Task 🔹 <a id="projen-tasks-task"></a>

A task that can be performed on the project.
Expand Down Expand Up @@ -7754,6 +7790,8 @@ Name | Type | Description
**packagingOptions**?🔹 | <code>[java.MavenPackagingOptions](#projen-java-mavenpackagingoptions)</code> | Packaging options.<br/>__*Default*__: defaults
**parent**?🔹 | <code>[Project](#projen-project)</code> | The parent project, if this project is part of a bigger project.<br/>__*Optional*__
**projectType**?🔹 | <code>[ProjectType](#projen-projecttype)</code> | Which type of project this is (library/app).<br/>__*Default*__: ProjectType.UNKNOWN
**projenrcJava**?🔹 | <code>boolean</code> | Use projenrc in java.<br/>__*Default*__: false
**projenrcJavaOptions**?🔹 | <code>[java.ProjenrcOptions](#projen-java-projenrcoptions)</code> | Options related to projenrc in java.<br/>__*Default*__: default options
**readme**?🔹 | <code>[SampleReadmeProps](#projen-samplereadmeprops)</code> | The README setup.<br/>__*Default*__: { filename: 'README.md', contents: '# replace this' }
**sample**?🔹 | <code>boolean</code> | Include sample code and test if the relevant directories don't exist.<br/>__*Optional*__
**sampleJavaPackage**?🔹 | <code>string</code> | The java package to use for the code sample.<br/>__*Default*__: "org.acme"
Expand Down Expand Up @@ -7867,6 +7905,21 @@ Name | Type | Description



## struct ProjenrcOptions 🔹 <a id="projen-java-projenrcoptions"></a>


Options for `Projenrc`.



Name | Type | Description
-----|------|-------------
**className**?🔹 | <code>string</code> | The name of the Java class which contains the `main()` method for projen.<br/>__*Default*__: "projenrc"
**projenVersion**?🔹 | <code>string</code> | The projen version to use.<br/>__*Default*__: current version
**testScope**?🔹 | <code>boolean</code> | Defines projenrc under the test scope instead of the main scope, which is reserved to the app.<br/>__*Default*__: true



## struct TaskCommonOptions 🔹 <a id="projen-tasks-taskcommonoptions"></a>


Expand Down
47 changes: 47 additions & 0 deletions docs/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,53 @@ Where `groupID` and `artifactId` are the Maven coordinates and `version` is the
semver syntax will be converted to POM syntax. For example, `^3.1.0` will be
converted to `[3.1.0,4.0.0)`.
## `projenrc.java`
It is possible to write your projenrc file in Java. In the future this will be
the default for Java projects, but at the moment this needs to be enabled when
the project is created:
```shell
$ projen new java --projenrc-java
```
Or set through:
```ts
new java.JavaProject({
// ...
projenrcJava: true
});
```
Then, create a file `src/test/java/projenrc.java` that looks like this:
```java
import org.projen.java.JavaProject;
import org.projen.java.JavaProjectOptions;

public class projenrc {
public static void main(String[] args) {
JavaProject project = new JavaProject(JavaProjectOptions.builder()
.name("my-app")
.groupId("org.acme")
.artifactId("my-app")
.version("1.0.0")
.build());

project.synth();
}
}
```
In order to synthesize, run: `pj synth`, which will compile your test code and
execute this program.
By default, `projenrc.java` is placed under the `test` scope (and
`com.github.eladb/projen` test dependency is added). This ensures that
application code does not take a dependency on projen code. You can change this
behavior by setting the `testScope` option to `false`.
## Maven Plugins
You can add Maven build plugins to your project using `project.addPlugin()`:
Expand Down
24 changes: 24 additions & 0 deletions src/__tests__/__snapshots__/inventory.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3454,6 +3454,30 @@ Array [
"switch": "project-type",
"type": "ProjectType",
},
Object {
"default": "false",
"docs": "Use projenrc in java.",
"name": "projenrcJava",
"optional": true,
"parent": "JavaProjectOptions",
"path": Array [
"projenrcJava",
],
"switch": "projenrc-java",
"type": "boolean",
},
Object {
"default": "- default options",
"docs": "Options related to projenrc in java.",
"name": "projenrcJavaOptions",
"optional": true,
"parent": "JavaProjectOptions",
"path": Array [
"projenrcJavaOptions",
],
"switch": "projenrc-java-options",
"type": "ProjenrcOptions",
},
Object {
"default": "- { filename: 'README.md', contents: '# replace this' }",
"docs": "The README setup.",
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/__snapshots__/new.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ const project = new java.JavaProject({
// junit: true, /* Include junit tests. */
// junitOptions: undefined, /* junit options. */
// packagingOptions: undefined, /* Packaging options. */
// projenrcJava: false, /* Use projenrc in java. */
// projenrcJavaOptions: undefined, /* Options related to projenrc in java. */
// sample: undefined, /* Include sample code and test if the relevant directories don't exist. */
// sampleJavaPackage: 'org.acme', /* The java package to use for the code sample. */
// testDeps: [], /* List of test dependencies for this project. */
Expand Down
Loading

0 comments on commit 9d099f6

Please sign in to comment.