使用Maven部署WebLogic应用
使用Maven部署WebLogic应用
在Maven项目中部署应用到WebLogic服务器可以通过以下几种方式实现:
1. 使用WebLogic Maven插件 (官方推荐)
Oracle提供了官方的WebLogic Maven插件,这是最直接的部署方式。
基本配置
<build><plugins><plugin><groupId>com.oracle.weblogic</groupId><artifactId>weblogic-maven-plugin</artifactId><version>12.2.1.4.0</version><configuration><adminurl>t3://localhost:7001</adminurl><user>weblogic</user><password>welcome1</password><upload>true</upload><remote>false</remote><verbose>true</verbose><source>${project.build.directory}/${project.build.finalName}.war</source><name>${project.build.finalName}</name></configuration><executions><execution><id>deploy</id><phase>deploy</phase><goals><goal>deploy</goal></goals></execution></executions></plugin></plugins>
</build>
常用命令
- 部署应用:
mvn weblogic:deploy
- 重新部署:
mvn weblogic:redeploy
- 取消部署:
mvn weblogic:undeploy
- 启动应用:
mvn weblogic:start
- 停止应用:
mvn weblogic:stop
2. 使用Cargo插件
Cargo提供了通用的应用服务器部署支持,包括WebLogic。
配置示例
<plugin><groupId>org.codehaus.cargo</groupId><artifactId>cargo-maven2-plugin</artifactId><version>1.9.11</version><configuration><container><containerId>weblogic12x</containerId><type>remote</type></container><configuration><type>runtime</type><properties><cargo.remote.uri>t3://localhost:7001</cargo.remote.uri><cargo.remote.username>weblogic</cargo.remote.username><cargo.remote.password>welcome1</cargo.remote.password></properties></configuration><deployables><deployable><groupId>${project.groupId}</groupId><artifactId>${project.artifactId}</artifactId><type>war</type><properties><context>${project.build.finalName}</context></properties></deployable></deployables></configuration>
</plugin>
常用命令
- 部署:
mvn cargo:deploy
- 重新部署:
mvn cargo:redeploy
- 取消部署:
mvn cargo:undeploy
3. 使用Ant任务通过Maven部署
结合maven-antrun-plugin和WebLogic的Ant任务:
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>3.1.0</version><executions><execution><phase>deploy</phase><goals><goal>run</goal></goals><configuration><target><taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy"classpath="${weblogic.home}/server/lib/weblogic.jar"/><wldeploy action="deploy"verbose="true"debug="false"name="${project.build.finalName}"source="${project.build.directory}/${project.build.finalName}.war"adminurl="t3://localhost:7001"user="weblogic"password="welcome1"targets="AdminServer"upload="true"/></target></configuration></execution></executions>
</plugin>
4. 使用WebLogic Deployer工具
WebLogic 12.2.1及以上版本提供了新的部署工具:
<plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>3.1.0</version><executions><execution><id>deploy-app</id><phase>deploy</phase><goals><goal>exec</goal></goals><configuration><executable>${weblogic.home}/oracle_common/common/bin/wlst.sh</executable><arguments><argument>${basedir}/src/main/scripts/deploy.py</argument><argument>${project.build.directory}/${project.build.finalName}.war</argument></arguments></configuration></execution></executions>
</plugin>
对应的deploy.py脚本示例:
connect('weblogic', 'welcome1', 't3://localhost:7001')
deploy('myapp', '/path/to/app.war', targets='AdminServer')
startApplication('myapp')
disconnect()
安全配置建议
-
不要在pom.xml中硬编码密码:
<configuration><user>${weblogic.username}</user><password>${weblogic.password}</password> </configuration>
然后在settings.xml中配置:
<servers><server><id>weblogic-server</id><username>weblogic</username><password>welcome1</password></server> </servers>
-
使用加密密码:
<password>{DES}encrypted_password</password>
多环境部署配置
<profiles><profile><id>dev</id><properties><weblogic.adminurl>t3://dev-server:7001</weblogic.adminurl></properties></profile><profile><id>prod</id><properties><weblogic.adminurl>t3://prod-server:7001</weblogic.adminurl></properties></profile>
</profiles>
使用命令指定环境:mvn deploy -Pprod
常见问题解决
-
ClassNotFoundException: weblogic.Deployer
确保weblogic.jar
在类路径中,可以通过设置weblogic.home
属性指定WebLogic安装目录。 -
认证失败
检查用户名/密码是否正确,确保用户有部署权限。 -
连接超时
检查WebLogic服务器是否运行,网络是否通畅,防火墙设置。 -
版本不兼容
确保插件版本与WebLogic服务器版本匹配。
以上方法可以根据项目需求选择使用,官方WebLogic Maven插件通常是最简单直接的选择。