xref: /aosp_15_r20/external/dagger2/util/deploy-library.sh (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1#!/bin/bash
2
3set -eu
4
5# Builds and deploys the given artifacts to a configured maven goal.
6# @param {string} library the library to deploy.
7# @param {string} pomfile the pom file to deploy.
8# @param {string} srcjar the sources jar of the library. This is an optional
9# parameter, if provided then javadoc must also be provided.
10# @param {string} javadoc the java doc jar of the library. This is an optional
11# parameter, if provided then srcjar must also be provided.
12# @param {string} module_name the JPMS module name to include in the jar. This
13# is an optional parameter and can only be used with jar files.
14deploy_library() {
15  local shaded_rules=$1
16  local library=$2
17  local pomfile=$3
18  local srcjar=$4
19  local javadoc=$5
20  local module_name=$6
21  local mvn_goal=$7
22  local version_name=$8
23  shift 8
24  local extra_maven_args=("$@")
25
26  bazel build --define=pom_version="$version_name" $library $pomfile
27
28  # Shade the library if shaded_rules exist
29  if [[ ! -z "$shaded_rules" ]]; then
30    bash $(dirname $0)/shade-library.sh \
31      $(bazel_output_file $library) $shaded_rules
32    # The output jar name is the same as the input library appended with -shaded
33    library="${library%.*}-shaded.${library##*.}"
34  fi
35
36  # Validate that the classes in the library jar begin with expected prefixes.
37  validate_jar $(bazel_output_file $library)
38
39  # TODO(bcorso): Consider moving this into the "gen_maven_artifact" macro, this
40  # requires having the version checked-in for the build system.
41  add_tracking_version \
42    $(bazel_output_file $library) \
43    $(bazel_output_file $pomfile) \
44    $version_name
45
46  # TODO(bcorso): Consider moving this into the "gen_maven_artifact" macro once
47  # all our targets are using gen_maven_artifact
48  add_automatic_module_name_manifest_entry \
49    $(bazel_output_file $library) \
50    "${module_name}"
51
52  if [ -n "$srcjar" ] && [ -n "$javadoc" ] ; then
53    bazel build --define=pom_version="$version_name" \
54      $srcjar $javadoc
55    mvn $mvn_goal \
56      -Dfile=$(bazel_output_file $library) \
57      -Djavadoc=$(bazel_output_file $javadoc) \
58      -DpomFile=$(bazel_output_file $pomfile) \
59      -Dsources=$(bazel_output_file $srcjar) \
60      "${extra_maven_args[@]:+${extra_maven_args[@]}}"
61  else
62    mvn $mvn_goal \
63      -Dfile=$(bazel_output_file $library) \
64      -DpomFile=$(bazel_output_file $pomfile) \
65      "${extra_maven_args[@]:+${extra_maven_args[@]}}"
66  fi
67}
68
69bazel_output_file() {
70  local library=$1
71  local output_file=bazel-bin/$library
72  if [[ ! -e $output_file ]]; then
73     output_file=bazel-genfiles/$library
74  fi
75  if [[ ! -e $output_file ]]; then
76    echo "Could not find bazel output file for $library"
77    exit 1
78  fi
79  echo -n $output_file
80}
81
82add_tracking_version() {
83  local library=$1
84  local pomfile=$2
85  local version_name=$3
86  local group_id=$(find_pom_value $pomfile "groupId")
87  local artifact_id=$(find_pom_value $pomfile "artifactId")
88  local temp_dir=$(mktemp -d)
89  local version_file="META-INF/${group_id}_${artifact_id}.version"
90  mkdir -p "$temp_dir/META-INF/"
91  echo $version_name >> "$temp_dir/$version_file"
92  if [[ $library =~ \.jar$ ]]; then
93    jar uf $library -C $temp_dir $version_file
94  elif [[ $library =~ \.aar$ ]]; then
95    unzip $library classes.jar -d $temp_dir
96    jar uf $temp_dir/classes.jar -C $temp_dir $version_file
97    jar uf $library -C $temp_dir classes.jar
98  else
99    echo "Could not add tracking version file to $library"
100    exit 1
101  fi
102}
103
104validate_jar() {
105  local library=$1
106  if [[ $library == */gwt/libgwt.jar ]]; then
107     python $(dirname $0)/validate-jar-entry-prefixes.py \
108        $library "dagger/,META-INF/,javax/inject/"
109  elif [[ $library == */java/dagger/hilt/android/artifact.aar ]]; then
110     python $(dirname $0)/validate-jar-entry-prefixes.py \
111        $library "dagger/,META-INF/,hilt_aggregated_deps/"
112  else
113     python $(dirname $0)/validate-jar-entry-prefixes.py \
114        $library "dagger/,META-INF/"
115  fi
116}
117
118add_automatic_module_name_manifest_entry() {
119  local library=$1
120  local module_name=$2
121  if [ -n "$module_name" ] ; then
122    if [[ $library =~ \.jar$ ]]; then
123      local temp_dir=$(mktemp -d)
124      echo "Automatic-Module-Name: ${module_name}" > $temp_dir/module_name_file
125      # The "m" flag is specifically for adding manifest entries.
126      jar ufm $library $temp_dir/module_name_file
127    else
128      echo "Could not add module name to $library"
129      exit 1
130    fi
131  fi
132}
133
134find_pom_value() {
135  local pomfile=$1
136  local attribute=$2
137  # Using Python here because `mvn help:evaluate` doesn't work with our gen pom
138  # files since they don't include the aar packaging plugin.
139  python $(dirname $0)/find_pom_value.py $pomfile $attribute
140}
141
142deploy_library "$@"
143