Tomcat7にEclipseからアプリケーションをデプロイする方法
前置き
Tomcat7に対して、EclipseからAntを使ってデプロイしようとした際に、色々苦戦したのでやり方を記録に残します。
やることはそれほど多くありませんが、Tomcat6とは違いがあるので、これからTomcat7を使う方は参考にどうぞ。
環境
手順
Tomcatの設定
- デプロイに使うTomcatユーザのロールに"manager- script"を追加します。
<tomcat-users> <user name="admin" password="tomcat" roles="manager-gui,manager-script"/> </tomcat-users>
- antが使用する4つのjarを取得
Eclipseの設定
Eclipseを使用せずに、Antのみで行いたい場合は、参考のページを見てください
- Antが使用するjarを追加
Build.xmlの編集
- 以下の内容をBuild.xmlに追加
- プロパティ値は、環境に合わせて書き換える
<!-- プロパティ値 --> <property name="manager.url" value="http://192.168.0.2:8080/manager/text"/> <property name="manager.username" value="admin"/> <property name="manager.password" value="tomcat"/> <property name="manager.context.path" value="/TomcatProject"/> <property name="manager.war.file" value="file:./dist/TomcatProject.war"/> <property name="manager.update" value="true"/> <target name="manager.deploy.war" depends="context.status" if="context.deployable"> <deploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${manager.context.path}" update="${manager.update}" war="${manager.war.file}"/> </target> <target name="manager.undeploy.war" description="context.status" if="context.undeployable"> <undeploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${manager.context.path}" /> </target> <target name="context.status"> <property name="running" value="${manager.context.path}:running"/> <property name="stopped" value="${manager.context.path}:stopped"/> <list url="${manager.url}" outputproperty="ctx.status" username="${manager.username}" password="${manager.password}"> </list> <condition property="context.running"> <contains string="${ctx.status}" substring="${running}"/> </condition> <condition property="context.stopped"> <contains string="${ctx.status}" substring="${stopped}"/> </condition> <condition property="context.notInstalled"> <and> <isfalse value="${context.running}"/> <isfalse value="${context.stopped}"/> </and> </condition> <condition property="context.deployable"> <or> <istrue value="${context.notInstalled}"/> <and> <istrue value="${context.running}"/> <istrue value="${manager.update}"/> </and> <and> <istrue value="${context.stopped}"/> <istrue value="${manager.update}"/> </and> </or> </condition> <condition property="context.undeployable"> <or> <istrue value="${context.running}"/> <istrue value="${context.stopped}"/> </or> </condition> </target>
- プロパティ値について
- manager.url
- デプロイ先のサーバを示すURL
- http://
<hostname>
:<port>
/manager/text
- http://
- デプロイ先のサーバを示すURL
- manager.username
- デプロイに使用するtomcatのユーザ名
- manager.password
- デプロイに使用するtomcatのパスワード
- manager.context.path
- デプロイするアプリケーションのコンテキストパス
- /<任意のコンテキストパス>
- /を忘れずにつける
- /<任意のコンテキストパス>
- デプロイするアプリケーションのコンテキストパス
- manager.war.file
- warへのパス
- manager.update
- すでにデプロイされている場合に、更新するか
- 更新する場合: true, 更新しない場合: false
- すでにデプロイされている場合に、更新するか
- manager.url
デプロイ
- プロジェクトエクスプローラ上で、Build.xmlを右クリック
- Run As → Ant Build を選択
- manager.deploy.warを選択し、Runを押下
アンデプロイ
- プロジェクトエクスプローラ上で、Build.xmlを右クリック
- Run As → Ant Build を選択
- manager.undeploy.warを選択し、Runを押下
以上、思ったより長かった