# Share code across repositories This section covers the basic workflow for sharing Java code between repositories. ## Prerequisites Before reading on, make sure you have created a separate repository containing the Java code / macros which you will depend on in other repositories. Throughout this guide, we will refer to this repository containing the Java code / macros to be shared as the *shared code repository*, and we will refer to any repository that depends on the *shared code repository* as a *dependent repository*. After adding Java code / macros to your *shared code repository*, make sure to compile the repository by committing your changes. ## Permissions Setup In order to share code, you must ensure that the *dependent repository* has access to the artifact produced by the *shared code repository*. To configure the artifacts that can be referenced from the *dependent repository*, navigate to the *Artifacts* section of the *Settings* tab. [Learn more about artifact settings.](../../code-repositories/artifact-settings/index.md) ## Gradle Setup ### For the Dependent Repository The Gradle setup for the *dependent repository* is required any time you are sharing code across repositories, including both Java code and macros. Jemma automatically publishes the artifact produced by the *shared code repository*. Thus, after setting up the required permissions mentioned above, all the *dependent repository* needs to do is point to the published artifact. That will involve adding a block like the following to the end of the `build.gradle` file in the language-specific subproject of the *dependent repository*: ```gradle dependencies { compile '::' } ``` Step through the process for getting the required information and adding it to the `build.gradle` file. 1. First, you need the `SHARED_CODE_REPO_RID`, that is, the RID of the *shared code repository*. You can obtain this from the repo URL. Alternatively, you can click on a repository entry on the Artifacts Settings tab to copy the above `compile` line with the shared repository RID filled in (such as `compile 'ri.stemma.repository.some-random-rid::'`) to your clipboard.

\ ![Artifacts settings tab](../../../assets/cce9b0de93d166f5f9da440db6fe5f6bca92a987e6d10c929d111d18fb7677c4.webp)

2. Next, you need to obtain values for `SHARED_CODE_REPO_GRADLE_SUBPROJECT` and `SHARED_CODE_REPO_VERSION`. To obtain these, modify the `ci.yml` file in the *shared code repository* by adding `--info` to the last line (starting with `./gradlew`). For example: ``` ./gradlew --no-daemon --build-cache --stacktrace patch publish ``` Becomes: ``` ./gradlew --no-daemon --build-cache --stacktrace patch publish --info ``` Then, refer to the CI output for the latest Jemma job in the *shared code repository*. Look for a line in the CI output that matches one of the following forms: ``` Upload https:///maven-repository-proxy/authz/user-code////-.jar ``` or ``` Uploading: ri/stemma/main/repository////-.jar to repository remote at https:///artifacts/api/legacy/mrp/authz/user-code/ ``` or ``` Uploading: ri/stemma/main/repository////-.jar to repository remote at https:///artifacts/api/repositories//contents/release/maven/ ``` Using the information from the CI output, you can construct the Maven coordinate referencing the artifact produced by your *shared code repository*. The `` you provide in your Maven coordinate should be of the form `ri.stemma.main.repository.{RID_VALUE}`. 3. Now that you have the Maven coordinate, you can update the `build.gradle` file. Make sure you edit the `build.gradle` file in the language-specific subproject folder (e.g. `transforms-java/build.gradle`), **not the one at the root of the repository**. Your updated `build.gradle` should look something like the following (note the two `dependencies` blocks): ```gradle buildscript { // ... dependencies { classpath "com.palantir.transforms.java:lang-java-gradle-plugin:${transformsJavaVersion}" } } apply plugin: 'com.palantir.transforms.lang.java' apply plugin: 'com.palantir.transforms.lang.java-defaults' dependencies { compile '::' } ``` You should now be able to access code from the *shared code repository* in the *dependent code repository*! :::callout{theme="warning" title="Shared code updates require a manual version bump"} As you update the code in the *shared code repository*, you will need to update the `` in your Maven coordinate to ensure that your *dependent repository* is using the most up-to-date version of the shared code. Each time you compile the *shared code repository*, check the CI output to find the updated version number you should be referencing. ::: ## Conclusion Now, you are ready to start sharing code across repositories. For any *dependent repository* that needs to reference code from the *shared code repository*, make sure you have taken the steps mentioned above to set up the correct permissions and dependencies. :::callout{theme="neutral"} Unlike with Python, Code Repositories does not support the creation of Java libraries, only the sharing of Java repositories. This means features such as tagging are also unsupported for Java. :::