Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 0 additions & 7 deletions examples/dependency-management/wintervegetables/BUILD

This file was deleted.

6 changes: 0 additions & 6 deletions examples/hello-world/wintervegetables/BUILD

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
# Compilation time only dependencies should not be evaluated by pomgen
# Compilation time only dependencies

There are some dependencies that are needed only at compilation time, for example Lombok. To ensure compile-time only dependencies are not included at runtime, Bazel has the `neverlink` attribute, which can be added to `java_library` rules.
Typically compile-time only dependencies won't have a `BUILD.pom` file, so they have to be ignored by `pomgen`.
There are some dependencies that are needed only at compilation time, for example Lombok. To ensure compile-time only dependencies are not included at runtime, Bazel has the `neverlink` attribute, which can be added to `java_library` rules. `poppy` honors the `neverlink` attribute and will not add jars produced by neverlinked `java_library` target to generated poms.


### Try this example

From the root of the repository:

```
bazel build examples/compile-only-dependencies/...
bazel build examples/java/compile-only-dependencies/...
```

```
bazel run @poppy//package/maven -- -a pomgen,install -l examples/compile-only-dependencies
bazel run @poppy//package/maven -- -a pomgen,install -l examples/java/compile-only-dependencies
```

It should pass although `examples/compile-only-dependencies/fancy` has no `BUILD.pom` file. The generated pom does not include the `neverlink` dependency.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
java_library(
name = "coollib",
srcs = glob(["src/main/java/**/*.java"]),
deps = ["//examples/compile-only-dependencies/fancy",
deps = ["//examples/java/compile-only-dependencies/fancy",
"@maven//:com_google_guava_guava",
]
)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Overridding dependencies

rules-jvm-external provides an [override mechanism](https://github.com/bazelbuild/rules_jvm_external?tab=readme-ov-file#overriding-generated-targets) that allows to switch out a group and artifact id with another dependency label. This features allows overriding dependencies that are brought in transitively (ie these dependencies are dragged in through top level dependencies referenced in bazel build files). pomgen honors the same override mechanism.
rules-jvm-external provides an [override mechanism](https://github.com/bazelbuild/rules_jvm_external?tab=readme-ov-file#overriding-generated-targets) that allows to switch out a group and artifact id with another dependency label. This features allows overriding dependencies that are brought in transitively (ie these dependencies are dragged in through top level dependencies referenced in bazel build files). poppy honors the same override mechanism.


## Example
Expand All @@ -16,18 +16,18 @@ The [library in this example](BUILD) depends on `org.antlr:ST4` (`@antlr//:org_a
Generate a pom.xml file for this example's library:

```
bazel run @pomgen//:pomgen -- --package examples/dep-overrides --destdir /tmp/overrides
bazel run @poppy//:pomgen -- --package examples/java/dep-overrides --destdir /tmp/overrides
```

Have a look at the generated pom.xml - note that the pom brings in the transitives listed above.


## Override a transitive

On order to override dependencies brought in transitively, pomgen needs to be pointed to one or more "overrides" file(s). Add to the `[general]` section of the [root .pomgenrc](../../.pomgenrc):
On order to override dependencies brought in transitively, pomgen needs to be pointed to one or more "overrides" file(s). Add to the `[general]` section of the [root .poppyrc](../../.poppyrc):

```
override_file_paths=examples/dep-overrides/overrides.bzl
override_file_paths=examples/java/dep-overrides/overrides.bzl
```

The content of the [overrides file](overrides.bzl) is:
Expand All @@ -41,7 +41,7 @@ This means that the `stringtemplate` dependency will be overridden with `guava`.
Regenerate the pom:

```
bazel run @poppy//:gen -- --package examples/dep-overrides --destdir /tmp/overrides
bazel run @poppy//:gen -- --package examples/java/dep-overrides --destdir /tmp/overrides
```

Have a look at the generated pom.xml - since the `stringtemplate` dependency is replaced by `guava`, the `antlr` dependency, previously dragged in by `stringtemplate`, is no longer there.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Dependency Management POM Example
# Dependency Management pom Example

This examples shows how pomgen can optionally generate a dependencyManagement "companion" pom. This pom file is generated in addition to the regular pom.xml, when the attribute `generate_dependency_management_pom` is to to `True` in the [BUILD.pom file](juicer/MVN-INF/BUILD.pom).
This examples shows how poppy can optionally generate a dependencyManagement "companion" pom. This pom file is generated in addition to the regular pom.xml, when the attribute `generate_dependency_management_pom` is to to `True` in the [BUILD.pom file](juicer/MVN-INF/BUILD.pom).

The dependency management pom contains a `<dependencyManagement>` section with the transitive closure of all dependencies of the artifact it was generated for. It uses the `artifact_id` specified in the BUILD.pom file, suffixed with `.depmanagement`.

Expand All @@ -10,11 +10,11 @@ The dependency management pom contains a `<dependencyManagement>` section with t
From the root of the repository:

```
bazel build examples/dependency-management/...
bazel build examples/java/dependency-management/...
```

```
bazel run @poppy//package/maven -- -a pomgen,install -l examples/dependency-management
bazel run @poppy//package/maven -- -a pomgen,install -l examples/java/dependency-management
```

Now look under your `$HOME/.m2/repository`:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
java_library(
name = "juicer",
srcs = glob(["src/main/java/**/*.java"]),
deps = ["//examples/dependency-management/wintervegetables"],
deps = ["//examples/java/dependency-management/wintervegetables"],
)
7 changes: 7 additions & 0 deletions examples/java/dependency-management/wintervegetables/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
java_library(
name = "wintervegetables",
srcs = glob(["src/main/java/**/*.java"]),
deps = ["//examples/java/dependency-management/healthyfoods/vegetable-api",
"@maven//:org_antlr_ST4"],
visibility = ["//examples/java/dependency-management/juicer:__subpackages__"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ The [metadata query](../../query.py) script provides information about Libraries
From the root of the repository:

```
bazel run @poppy//:query -- --package examples/hello-world/juicer --library_release_plan_tree
bazel run @poppy//:query -- --package examples/java/hello-world/juicer --library_release_plan_tree
```

The output looks similar to this:
```
examples/hello-world/juicer ++ 3.0.0-SNAPSHOT
examples/hello-world/healthyfoods ++ 1.0.0-SNAPSHOT
examples/hello-world/wintervegetables ++ 2.0.0-SNAPSHOT
examples/hello-world/healthyfoods ++ 1.0.0-SNAPSHOT
examples/java/hello-world/juicer ++ 3.0.0-SNAPSHOT
examples/java/hello-world/healthyfoods ++ 1.0.0-SNAPSHOT
examples/java/hello-world/wintervegetables ++ 2.0.0-SNAPSHOT
examples/java/hello-world/healthyfoods ++ 1.0.0-SNAPSHOT
```

The output shows:
Expand All @@ -61,18 +61,18 @@ The output shows:
pomgen can be run directly (`bazel run @poppy//:pomgen -- --help`), however, there's a convenient [wrapper script](../../package/maven/maven.sh) that we'll use instead: it defaults some pomgen flags. Before running, make sure you have the required [external dependencies](../../package/maven/README.md#external-dependencies).

```
bazel run @poppy//package/maven -- -a pomgen -l examples/hello-world/juicer
bazel run @poppy//package/maven -- -a pomgen -l examples/java/hello-world/juicer
```

The command above specifies:
- The **action** to run, `pomgen` in this case (to generate poms)
- The **library** to generate poms for: `examples/hello-world/juicer`
- The **library** to generate poms for: `examples/java/hello-world/juicer`


pomgen follows references between libraries; since `juicer` depends on 2 other libraries `healthyfoods` and `wintervegerables`, pomgen generated pom.xml files for all 3 libraries, ie for all modules that are part of those libraries. Usually this is the right behavior, but if there a lot of upstream libraries, it may be desirable in some cases to not follow library references. This can be accomplished by setting `-i` (ignore references) flag:

```
bazel run @poppy//package/maven -- -a pomgen -l examples/hello-world/juicer -i
bazel run @poppy//package/maven -- -a pomgen -l examples/java/hello-world/juicer -i
```


Expand All @@ -81,31 +81,31 @@ bazel run @poppy//package/maven -- -a pomgen -l examples/hello-world/juicer -i
The Maven Artifacts generated by pomgen can be installed into `~/.m2/repository` so that they are consumable by a local Maven project. Use the `install` action:

```
bazel run @poppy//package/maven -- -a install -l examples/hello-world/juicer
bazel run @poppy//package/maven -- -a install -l examples/java/hello-world/juicer
```

Note that before running `-a install`, pom.xml files must have been generated: `-a pomgen` must have run.

If you see this type of error:

```
ERROR: did not find jar artifact at /Users/stoens/Code/pomgen/bazel-bin/examples/hello-world/juicer/juicer.jar
ERROR: did not find jar artifact at /Users/stoens/Code/pomgen/bazel-bin/examples/java/hello-world/juicer/juicer.jar
```

This error means that pomgen did not find the jar file for the juicer module - this happens when `bazel build` did not run. There are 2 solutions:
- Run `bazel build examples/hello-world/...`
- If there are many upstream libraries in a large repository, it may be helpful to ask pomgen to build all upstream libraries - use the `build` action: `bazel run @poppy//package/maven -- -a build -l examples/hello-world/juicer`
- Run `bazel build examples/java/hello-world/...`
- If there are many upstream libraries in a large repository, it may be helpful to ask pomgen to build all upstream libraries - use the `build` action: `bazel run @poppy//package/maven -- -a build -l examples/java/hello-world/juicer`

Finally, note that `-a` can take multiple actions, so the above can also be run like this:

```
bazel run @poppy//package/maven -- -a pomgen,build,install -l examples/hello-world/juicer
bazel run @poppy//package/maven -- -a pomgen,build,install -l examples/java/hello-world/juicer
```


### Referencing Bazel-built jars in a Maven projects

Once the `install` action ran successfully for `examples/hello-world/juicer`, you can use the `juicer` artifact in a Maven project. Try this pom.xml:
Once the `install` action ran successfully for `examples/java/hello-world/juicer`, you can use the `juicer` artifact in a Maven project. Try this pom.xml:

```
<project>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
java_library(
name = "juicer_lib",
srcs = glob(["src/main/java/**/*.java"]),
deps = ["//examples/hello-world/healthyfoods/fruit-api",
"//examples/hello-world/healthyfoods/vegetable-api",
"//examples/hello-world/wintervegetables:wintervegetables_lib2",
deps = ["//examples/java/hello-world/healthyfoods/fruit-api",
"//examples/java/hello-world/healthyfoods/vegetable-api",
"//examples/java/hello-world/wintervegetables:wintervegetables_lib2",
"@maven//:com_google_guava_guava",
]
)
Expand Down
6 changes: 6 additions & 0 deletions examples/java/hello-world/wintervegetables/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
java_library(
name = "wintervegetables_lib2",
srcs = glob(["src/main/java/**/*.java"]),
deps = ["//examples/java/hello-world/healthyfoods/vegetable-api"],
visibility = ["//examples/java/hello-world/juicer:__subpackages__"],
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# java_import

Limited support for `java_import` usage exists, limited because of pomgen's assumption that there's a 1-1 mapping between Bazel Package and produced jar artifact.
Limited support for `java_import` usage exists, limited because of poppy's assumption that there's a 1-1 mapping between Bazel Package and produced jar artifact.

In practice, this means that the `java_import` rule can only reference a single jar file ([example](oldfruit/BUILD)). The corresponding [BUILD.pom](oldfruit/MVN-INF/BUILD.pom) file must specify the `jar_path` attribute.

Expand All @@ -10,14 +10,14 @@ In practice, this means that the `java_import` rule can only reference a single
From the root of the repository:

```
bazel build examples/java-import/...
bazel build examples/java/java-import/...
```

```
bazel run @poppy//package/maven -- -a pomgen,install -l examples/java-import
bazel run @poppy//package/maven -- -a pomgen,install -l examples/java/java-import
```

The cmd above installs jars and poms into `~/.m2/repository`. The `java_import` jar is at:
```
~/.m2/repository/com/pomgen/example/oldfruit/1.0.0-SNAPSHOT/oldfruit-1.0.0-SNAPSHOT.jar
~/.m2/repository/com/pomgen/example/java/oldfruit/1.0.0-SNAPSHOT/oldfruit-1.0.0-SNAPSHOT.jar
```
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ java_import(
jars = [
"oldfruit.jar",
],
visibility = ["//examples/java-import/oldfruitlover:__pkg__"],
visibility = ["//examples/java/java-import/oldfruitlover:__pkg__"],
)

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
artifact(
group_id = "com.pomgen.example",
group_id = "com.pomgen.example.java",
artifact_id = "oldfruit",
version = "1.0.0-SNAPSHOT",
generation_mode = "dynamic",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
java_library(
name = "oldfruitlover",
srcs = glob(["src/main/java/**/*.java"]),
deps = ["//examples/java-import/oldfruit"],
deps = ["//examples/java/java-import/oldfruit"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

## Looking around

The [skip pom generation mode](passthrough/MVN-INF/BUILD.pom) can be used to mark bazel targets that [provide dependencies only](passthrough/BUILD), but that do not produce any Maven artifacts. All dependencies of these types of targets will be added to pom generated for the referencing target.
The [skip pom generation mode](passthrough/MVN-INF/BUILD.pom) can be used to mark bazel targets that [provide dependencies only](passthrough/BUILD), but that do not produce any artifacts. All dependencies of these types of targets will be added to manifest generated for the referencing target.

The libraries in this example reference each other the following way:

Expand All @@ -17,7 +17,7 @@ parent (contains 2 artifacts: parent1 and parent2) -> passthrough -> lib
From the root of the repository:

```
bazel run @pomgen//:pomgen -- --package examples/skip-artifact-generation/parent --destdir /tmp/pomgen
bazel run @poppy//:pomgen -- --package examples/java/skip-artifact-generation/parent --destdir /tmp/pomgen
```

The command above generates 3 poms, for:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ java_library(
name = "lib",
srcs = glob(["src/main/java/**/*.java"]),
runtime_deps = ["@maven//:org_apache_commons_commons_math3"],
visibility = ["//examples/skip-artifact-generation/passthrough:__subpackages__"],
visibility = ["//examples/java/skip-artifact-generation/passthrough:__subpackages__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
released_maven_artifact(
version = "0.0.1",
artifact_hash = "ec2c3b6c895c18ee0331954c85920859de9765bd",
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ java_library(
srcs = glob(["src/main/java/**/*.java"]),
# these deps get pulled into the pom generated for *this* package
# because the reference below has pomgen mode == skip
deps = ["//examples/skip-artifact-generation/passthrough:set1"],
deps = ["//examples/java/skip-artifact-generation/passthrough:set1"],
runtime_deps = ["@maven//:org_antlr_stringtemplate"],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ java_library(
srcs = glob(["src/main/java/**/*.java"]),
# these deps get pulled into the pom generated for *this* package
# because the reference below has pomgen mode == skip
deps = ["//examples/skip-artifact-generation/passthrough:set1"],
deps = ["//examples/java/skip-artifact-generation/passthrough:set1"],
runtime_deps = ["@maven//:org_antlr_ST4"],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ java_library(

java_library(
name = "set2",
runtime_deps = [":set3", "//examples/skip-artifact-generation/lib",],
runtime_deps = [":set3", "//examples/java/skip-artifact-generation/lib",],
)

java_library(
Expand Down

This file was deleted.

Empty file.
2 changes: 1 addition & 1 deletion src/crawl/crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ def _filter_label(self, label, downstream_artifact_def):
if bazel.is_never_link_dep(self.workspace.repo_root_path, label.canonical_form):
return None
else:
raise Exception("cannot process this package because there is no metadata directory: [%s]" % label.package_path)
raise Exception("cannot process this package because there is no manifest metadata at: [%s]" % label.package_path)
return label

def _store_if_leafnode(self, node):
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.pomgen.example</groupId>
<groupId>com.pomgen.example.java</groupId>
<artifactId>oldfruit</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
examples/java/java-import/oldfruit/oldfruit.jar
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<dependencies>
<dependency>
<groupId>com.pomgen.example</groupId>
<groupId>com.pomgen.example.java</groupId>
<artifactId>oldfruit</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
Expand Down