···49After setting `maven.buildMavenPackage`, we then do standard Java `.jar` installation by saving the `.jar` to `$out/share/java` and then making a wrapper which allows executing that file; see [](#sec-language-java) for additional generic information about packaging Java applications.
50:::
5100000000000000000000000000000000000000000000000000000000000000000000000000000000000052### Stable Maven plugins {#stable-maven-plugins}
5354Maven defines default versions for its core plugins, e.g. `maven-compiler-plugin`. If your project does not override these versions, an upgrade of Maven will change the version of the used plugins, and therefore the derivation and hash.
···49After setting `maven.buildMavenPackage`, we then do standard Java `.jar` installation by saving the `.jar` to `$out/share/java` and then making a wrapper which allows executing that file; see [](#sec-language-java) for additional generic information about packaging Java applications.
50:::
5152+### Overriding Maven package attributes {#maven-overriding-package-attributes}
53+54+```
55+overrideMavenAttrs :: (AttrSet -> Derivation) | ((AttrSet -> Attrset) -> Derivation) -> Derivation
56+```
57+58+The output of `buildMavenPackage` has an `overrideMavenAttrs` attribute, which is a function that takes either
59+- any subset of the attributes that can be passed to `buildMavenPackage`
60+61+ or
62+- a function that takes the argument passed to the previous invocation of `buildMavenPackage` (conventionally called `old`) and returns an attribute set that can be passed to `buildMavenPackage`
63+64+and returns a derivation that builds a Maven package based on the old and new arguments merged.
65+66+This is similar to [](#sec-pkg-overrideAttrs), but notably does not allow accessing the final value of the argument to `buildMavenPackage`.
67+68+:::{.example}
69+### `overrideMavenAttrs` Example
70+71+Use `overrideMavenAttrs` to build `jd-cli` version 1.2.0 and disable some flaky test:
72+73+```nix
74+jd-cli.overrideMavenAttrs (old: rec {
75+ version = "1.2.0";
76+ src = fetchFromGitHub {
77+ owner = old.src.owner;
78+ repo = old.src.repo;
79+ rev = "${old.pname}-${version}";
80+ # old source hash of 1.2.0 version
81+ hash = "sha256-US7j6tQ6mh1libeHnQdFxPGoxHzbZHqehWSgCYynKx8=";
82+ };
83+84+ # tests can be disabled by prefixing it with `!`
85+ # see Maven documentation for more details:
86+ # https://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html#Multiple_Formats_in_One
87+ mvnParameters = lib.escapeShellArgs [
88+ "-Dsurefire.failIfNoSpecifiedTests=false"
89+ "-Dtest=!JavaDecompilerTest#basicTest,!JavaDecompilerTest#patternMatchingTest"
90+ ];
91+92+ # old mvnHash of 1.2.0 maven dependencies
93+ mvnHash = "sha256-N9XC1pg6Y4sUiBWIQUf16QSXCuiAPpXEHGlgApviF4I=";
94+});
95+```
96+:::
97+98+### Offline build {#maven-offline-build}
99+100+By default, `buildMavenPackage` does the following:
101+102+1. Run `mvn package -Dmaven.repo.local=$out/.m2 ${mvnParameters}` in the
103+ `fetchedMavenDeps` [fixed-output derivation](https://nixos.org/manual/nix/stable/glossary.html#gloss-fixed-output-derivation).
104+2. Run `mvn package -o -nsu "-Dmaven.repo.local=$mvnDeps/.m2"
105+ ${mvnParameters}` again in the main derivation.
106+107+As a result, tests are run twice.
108+This also means that a failing test will trigger a new attempt to realise the fixed-output derivation, which in turn downloads all dependencies again.
109+For bigger Maven projects, this might lead to a long feedback cycle.
110+111+Use `buildOffline = true` to change the behaviour of `buildMavenPackage to the following:
112+1. Run `mvn de.qaware.maven:go-offline-maven-plugin:1.2.8:resolve-dependencies
113+ -Dmaven.repo.local=$out/.m2 ${mvnDepsParameters}` in the fixed-output derivation.
114+2. Run `mvn package -o -nsu "-Dmaven.repo.local=$mvnDeps/.m2"
115+ ${mvnParameters}` in the main derivation.
116+117+As a result, all dependencies are downloaded in step 1 and the tests are executed in step 2.
118+A failing test only triggers a rebuild of step 2 as it can reuse the dependencies of step 1 because they have not changed.
119+120+::: {.warning}
121+Test dependencies are not downloaded in step 1 and are therefore missing in
122+step 2 which will most probably fail the build. The `go-offline` plugin cannot
123+handle these so-called [dynamic dependencies](https://github.com/qaware/go-offline-maven-plugin?tab=readme-ov-file#dynamic-dependencies).
124+In that case you must add these dynamic dependencies manually with:
125+```nix
126+maven.buildMavenPackage rec {
127+ manualMvnArtifacts = [
128+ # add dynamic test dependencies here
129+ "org.apache.maven.surefire:surefire-junit-platform:3.1.2"
130+ "org.junit.platform:junit-platform-launcher:1.10.0"
131+ ];
132+};
133+```
134+:::
135+136### Stable Maven plugins {#stable-maven-plugins}
137138Maven defines default versions for its core plugins, e.g. `maven-compiler-plugin`. If your project does not override these versions, an upgrade of Maven will change the version of the used plugins, and therefore the derivation and hash.