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.
12deploy_library() {
13  local library=$1
14  local pomfile=$2
15  local srcjar=$3
16  local javadoc=$4
17  local mvn_goal=$5
18  local version_name=$6
19  shift 6
20  local extra_maven_args=("$@")
21
22  bazel build --define=pom_version="$version_name" \
23    $library $pomfile
24
25  # TODO(bcorso): Consider moving this into the "gen_maven_artifact" macro, this
26  # requires having the version checked-in for the build system.
27  add_tracking_version \
28    $(bazel_output_file $library) \
29    $(bazel_output_file $pomfile) \
30    $version_name
31
32  if [ -n "$srcjar" ] && [ -n "$javadoc" ] ; then
33    bazel build --define=pom_version="$version_name" \
34      $srcjar $javadoc
35    mvn $mvn_goal \
36      -Dfile=$(bazel_output_file $library) \
37      -Djavadoc=$(bazel_output_file $javadoc) \
38      -DpomFile=$(bazel_output_file $pomfile) \
39      -Dsources=$(bazel_output_file $srcjar) \
40      "${extra_maven_args[@]:+${extra_maven_args[@]}}"
41  else
42    mvn $mvn_goal \
43      -Dfile=$(bazel_output_file $library) \
44      -DpomFile=$(bazel_output_file $pomfile) \
45      "${extra_maven_args[@]:+${extra_maven_args[@]}}"
46  fi
47}
48
49bazel_output_file() {
50  local library=$1
51  local output_file=bazel-bin/$library
52  if [[ ! -e $output_file ]]; then
53     output_file=bazel-genfiles/$library
54  fi
55  if [[ ! -e $output_file ]]; then
56    echo "Could not find bazel output file for $library"
57    exit 1
58  fi
59  echo -n $output_file
60}
61
62add_tracking_version() {
63  local library=$1
64  local pomfile=$2
65  local version_name=$3
66  local group_id=$(find_pom_value $pomfile "groupId")
67  local artifact_id=$(find_pom_value $pomfile "artifactId")
68  local temp_dir=$(mktemp -d)
69  local version_file="META-INF/${group_id}_${artifact_id}.version"
70  mkdir -p "$temp_dir/META-INF/"
71  echo $version_name >> "$temp_dir/$version_file"
72  if [[ $library =~ \.jar$ ]]; then
73    jar uf $library -C $temp_dir $version_file
74  elif [[ $library =~ \.aar$ ]]; then
75    unzip $library classes.jar -d $temp_dir
76    jar uf $temp_dir/classes.jar -C $temp_dir $version_file
77    jar uf $library -C $temp_dir classes.jar
78  else
79    echo "Could not add tracking version file to $library"
80    exit 1
81  fi
82}
83
84find_pom_value() {
85  local pomfile=$1
86  local attribute=$2
87  # Using Python here because `mvn help:evaluate` doesn't work with our gen pom
88  # files since they don't include the aar packaging plugin.
89  python $(dirname $0)/find_pom_value.py $pomfile $attribute
90}
91
92deploy_library "$@"
93