読者です 読者をやめる 読者になる 読者になる

あまりシェルとか書いた事ないんだけれども。。。

mavenでよくレポジトリにファイルがないjarとか、ローカルのディレクトリをリポジトリとして指定するとかやるんですが、mavenリポジトリ構成を作るのがわりとめんどいので、簡単なシェルを書いてみた。あんまり書いた事ないから自信はない!

#!/bin/sh

OUTPUT_DIR="target"

function create_md5_file(){
  MD5_TMP=`openssl md5 $1`
  echo ${MD5_TMP##* } > $1.md5
}

function write_pom_file(){
  echo "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" >> $1
  echo "  xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">" >> $1
  echo "  <modelVersion>4.0.0</modelVersion>" >> $1
  echo "  <groupId>$2</groupId>" >> $1
  echo "  <artifactId>$3</artifactId>" >> $1
  echo "  <packaging>jar</packaging>" >> $1
  echo "  <version>$4</version>" >> $1
  echo "</project>" >> $1
  create_md5_file $1
}

if [ $# -lt 1 -o $# -gt 2 ]; then
  echo "Usage: mkmavendir same.jar [same-sources.jar]"
  exit 1
fi

ARTIFACT_MODULE=$1
ARTIFACT_SOURCES=$2

if [ ! -r "$ARTIFACT_MODULE" ]; then
  echo "$ARTIFACT_MODULE" "is not found"
  exit 1
fi
if [ -n "$2" ]; then
  if [ ! -r "$ARTIFACT_SOURCES" ]; then  
    echo "$ARTIFACT_SOURCES" "is not found"
    exit 1
  fi
fi

echo -n "Define value for groupId: "
read GROUP_ID
echo -n "Define value for artifactId: " 
read ARTIFACT_ID
echo -n "Define value for version: "
read VERSION
echo "groupId: ${GROUP_ID}"
echo "artifactId: ${ARTIFACT_ID}"
echo "version: ${VERSION}"

echo -n "ok?[y/n]: "
read CONTINUE

if [ "$CONTINUE" != "y" -a "$CONTINUE" != "Y" ]; then
  exit 1
fi

echo "[INFO] create artifact directory" 
ARTIFACT_DIR=${OUTPUT_DIR}/${GROUP_ID//.//}/${ARTIFACT_ID}/${VERSION}
mkdir -p ${ARTIFACT_DIR}

echo "[INFO] copy artifact module" 
cp ${ARTIFACT_MODULE} ${ARTIFACT_DIR}/${ARTIFACT_ID}-${VERSION}.jar
create_md5_file ${ARTIFACT_DIR}/${ARTIFACT_ID}-${VERSION}.jar

echo "[INFO] copy artifact sources" 
if [ -n "$2" ]; then
  cp ${ARTIFACT_SOURCES} ${ARTIFACT_DIR}/${ARTIFACT_ID}-${VERSION}-sources.jar
  create_md5_file ${ARTIFACT_DIR}/${ARTIFACT_ID}-${VERSION}-sources.jar
fi

echo "[INFO] generate artifact pom" 
ARTIFACT_POM=${ARTIFACT_DIR}/${ARTIFACT_ID}-${VERSION}.pom
touch ${ARTIFACT_POM}
write_pom_file ${ARTIFACT_POM} ${GROUP_ID} ${ARTIFACT_ID} ${VERSION}

echo "[INFO] ------------------------------------------------------------------------"
echo "[INFO] SUCCESSFUL"
echo "[INFO] ------------------------------------------------------------------------"

exit $?

なんとなくmaven風の出力にしてみた。

使いは

$ ./mkmavendir.sh hoge.jar hoge-sources.jar 
Define value for groupId: : aaa.bbb
Define value for artifactId: : ccc
Define value for version: : 1.0
groupId: aaa.bbb
artifactId: ccc
version: 1.0
ok?[y/n]: :y
[INFO] create artifact directory
[INFO] copy artifact module
[INFO] copy artifact sources
[INFO] generate artifact pom
[INFO] ------------------------------------------------------------------------
[INFO] SUCCESSFUL
[INFO] ------------------------------------------------------------------------
$ ls target/aaa/bbb/ccc/1.0/
ccc-1.0-sources.jar     ccc-1.0-sources.jar.md5 ccc-1.0.jar             ccc-1.0.jar.md5         ccc-1.0.pom             ccc-1.0.pom.md5

あ、macでしか動作確認してません!