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

Produced source JARs don't correspond to files in class JAR #8277

Closed
vmax opened this issue May 9, 2019 · 6 comments
Closed

Produced source JARs don't correspond to files in class JAR #8277

vmax opened this issue May 9, 2019 · 6 comments
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-Java Issues for Java rules

Comments

@vmax
Copy link
Contributor

vmax commented May 9, 2019

Description of the problem / feature request:

Source JARs produced currently replicate the structure of source files used to build the class JAR. Example:

ghost:c vmax$ bazel build //:libanotherlib-src.jar
…
Target //:libanotherlib-src.jar up-to-date:
  bazel-bin/libanotherlib-src.jar
ghost:c vmax$ jar tf bazel-bin/libanotherlib-src.jar
META-INF/
META-INF/MANIFEST.MF
Anotherlib.java

Given this example, I would expect source JAR to contain structure similar to the following:

META-INF/
META-INF/MANIFEST.MF
com/
com/smth/
com/smth/Anotherlib.java

Feature requests: what underlying problem are you trying to solve with this feature?

We need produced source JARs to have the same structure as class files inside class JAR so that:

  • produced source JARs could be deployed to Maven Central
  • "Go to source" functionality works with IntelliJ IDEA + Bazel plugin

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

ghost:c vmax$ cat BUILD 
java_library(
    name = "anotherlib",
    srcs = ["Anotherlib.java"],
    visibility = ["//visibility:public"]
)
ghost:c vmax$ cat AnotherLib.java 
package com.smth;

public class Anotherlib {
    public Anotherlib() {
        System.err.println("ctor");
    }
}

What operating system are you running Bazel on?

macOS Mojave 10.14.4

What's the output of bazel info release?

release 0.25.0

Have you found anything relevant by searching the web?

@davido
Copy link
Contributor

davido commented May 9, 2019

Create java/com/smth directory, move there your Anotherlib.java and change your BUILD file like this:

java_library(
    name = "anotherlib",
    srcs = ["java/com/smth/Anotherlib.java"],
    visibility = ["//visibility:public"]
)

Check Gerrit Code Review: [1]. We do it like I described above and we put JARs and source-JARs created by Bazel to Maven Central:

  $ bazel build java/com/google/gerrit/common:server

and check the structure of the resulted JAR an source-JAR.

[1] https://github.com/GerritCodeReview/gerrit/blob/master/java/com/google/gerrit/common/BUILD

@vmax
Copy link
Contributor Author

vmax commented May 9, 2019

@davido thank you for your suggestion! however, my intention is to preserve the directory structure we currently have where directories represent logical separation of modules and not necessarily java packaging

@irengrig irengrig added team-Rules-Java Issues for Java rules untriaged labels May 12, 2019
@vmax
Copy link
Contributor Author

vmax commented May 22, 2019

We were able to resolve it by wrapping singlejar in a Java wrapper (src) and using custom java_toolchain (src)

@iirina iirina added P3 We're not considering working on this, but happy to review a PR. (No assignee) and removed untriaged labels May 31, 2019
vmax added a commit to vaticle/typedb that referenced this issue Sep 23, 2019
## What is the goal of this PR?

Bazel upgrade to `0.29.0` planned across all repos is going to break all CI because newest version doesn't work with our custom Java toolchain (due to bazelbuild/bazel#8659). However, we found a different way to approach the original problem (described in bazelbuild/bazel#8277) which is modifying the IntelliJ Bazel plugin. This solution does not require custom Java toolchain and therefore it'll be removed.

## What are the changes implemented in this PR?

Stop using custom `java_toolchain` [with modified `singlejar` binary] for all builds
@vmax
Copy link
Contributor Author

vmax commented Jan 30, 2020

We've addressed it by using a workaround on plugin level: bazelbuild/intellij#1206, bazelbuild/intellij#1207

@vmax vmax closed this as completed Jan 30, 2020
@davido
Copy link
Contributor

davido commented Feb 1, 2020

@vmax Can you re-open this issue? Alternatively, I can open a new issue for source path normalization in created "-src.jar" in java_library rule.

I am facing a similar problem, when trying to migrate existing project to Bazel an I think, that this should be fixed in Bazel.

Consider this directory structure (that I cannot change):

<workspace_root>/foo/org/example/app/Test.java

The build file is put in foo directory. The java_library rule is:

java_library(
  name = "foo",
  srcs = glob(["org/**/*.java"]),
)

When building foo the java library is correct, but the libfoo-src.jar is wrong: it has this entry:

"foo/org/example/app/Test.java"
instead
"org/example/app/Test.java"

Note, that if I rename the foo directory to one of these directories:

  • java
  • src
  • src/main/java

then the "foo" directory is stripped. One approach to fix that would be to provide source_strip_prefix in java_library rule or apply resource_strip_prefix on path normalization in "-src.jar" file.

One workaround, that can be done now, is to create srcjar manually and provide it as source to the java_library rule. Using java_binary, the sources can be provided as resources and the path can be normalized using resource_strip_prefix=foo, so something like:

# TODO(davido): Remove that hack to create source package without the wrong path prefix.
java_binary(
    name = "foo-src-binary",
    main_class = "Dummy.java",
    resources = glob(["org/**/*.java"]),
    resource_strip_prefix = "foo",
)

# TODO(davido): Remove that hack to rename the java_binary source with ".jar" suffix
# to zip file with ".srcjar" suffix so that java_library can accept it.
genrule2(
    name = "foo-src",
    cmd = "mv $< $@",
    srcs = [":foo-src-binary_deploy.jar"],
    outs = ["foo.srcjar"],
)

java_library(
    name = "foo",
    srcs = [":foo-src"],
)

@vmax
Copy link
Contributor Author

vmax commented Feb 1, 2020

@davido I think you should open another one: you might have another reasons for wanting to have a correct source JAR and will then be able to convince Bazel team to look at them 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-Java Issues for Java rules
Projects
None yet
Development

No branches or pull requests

4 participants