• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  # TODO(bcorso): Consider moving this into the "gen_maven_artifact" macro, this
37  # requires having the version checked-in for the build system.
38  add_tracking_version \
39    $(bazel_output_file $library) \
40    $(bazel_output_file $pomfile) \
41    $version_name
42
43  # TODO(bcorso): Consider moving this into the "gen_maven_artifact" macro once
44  # all our targets are using gen_maven_artifact
45  add_automatic_module_name_manifest_entry \
46    $(bazel_output_file $library) \
47    "${module_name}"
48
49  if [ -n "$srcjar" ] && [ -n "$javadoc" ] ; then
50    bazel build --define=pom_version="$version_name" \
51      $srcjar $javadoc
52    mvn $mvn_goal \
53      -Dfile=$(bazel_output_file $library) \
54      -Djavadoc=$(bazel_output_file $javadoc) \
55      -DpomFile=$(bazel_output_file $pomfile) \
56      -Dsources=$(bazel_output_file $srcjar) \
57      "${extra_maven_args[@]:+${extra_maven_args[@]}}"
58  else
59    mvn $mvn_goal \
60      -Dfile=$(bazel_output_file $library) \
61      -DpomFile=$(bazel_output_file $pomfile) \
62      "${extra_maven_args[@]:+${extra_maven_args[@]}}"
63  fi
64}
65
66bazel_output_file() {
67  local library=$1
68  local output_file=bazel-bin/$library
69  if [[ ! -e $output_file ]]; then
70     output_file=bazel-genfiles/$library
71  fi
72  if [[ ! -e $output_file ]]; then
73    echo "Could not find bazel output file for $library"
74    exit 1
75  fi
76  echo -n $output_file
77}
78
79add_tracking_version() {
80  local library=$1
81  local pomfile=$2
82  local version_name=$3
83  local group_id=$(find_pom_value $pomfile "groupId")
84  local artifact_id=$(find_pom_value $pomfile "artifactId")
85  local temp_dir=$(mktemp -d)
86  local version_file="META-INF/${group_id}_${artifact_id}.version"
87  mkdir -p "$temp_dir/META-INF/"
88  echo $version_name >> "$temp_dir/$version_file"
89  if [[ $library =~ \.jar$ ]]; then
90    jar uf $library -C $temp_dir $version_file
91  elif [[ $library =~ \.aar$ ]]; then
92    unzip $library classes.jar -d $temp_dir
93    jar uf $temp_dir/classes.jar -C $temp_dir $version_file
94    jar uf $library -C $temp_dir classes.jar
95  else
96    echo "Could not add tracking version file to $library"
97    exit 1
98  fi
99}
100
101add_automatic_module_name_manifest_entry() {
102  local library=$1
103  local module_name=$2
104  if [ -n "$module_name" ] ; then
105    if [[ $library =~ \.jar$ ]]; then
106      local temp_dir=$(mktemp -d)
107      echo "Automatic-Module-Name: ${module_name}" > $temp_dir/module_name_file
108      # The "m" flag is specifically for adding manifest entries.
109      jar ufm $library $temp_dir/module_name_file
110    else
111      echo "Could not add module name to $library"
112      exit 1
113    fi
114  fi
115}
116
117find_pom_value() {
118  local pomfile=$1
119  local attribute=$2
120  # Using Python here because `mvn help:evaluate` doesn't work with our gen pom
121  # files since they don't include the aar packaging plugin.
122  python $(dirname $0)/find_pom_value.py $pomfile $attribute
123}
124
125deploy_library "$@"
126