使用Maven部署WebLogic应用

使用Maven部署WebLogic应用

在Maven项目中部署应用到WebLogic服务器可以通过以下几种方式实现:

1. 使用WebLogic Maven插件 (官方推荐)

Oracle提供了官方的WebLogic Maven插件,这是最直接的部署方式。

基本配置

xml 复制代码
<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。

配置示例

xml 复制代码
<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任务:

xml 复制代码
<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及以上版本提供了新的部署工具:

xml 复制代码
<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脚本示例:

python 复制代码
connect('weblogic', 'welcome1', 't3://localhost:7001')
deploy('myapp', '/path/to/app.war', targets='AdminServer')
startApplication('myapp')
disconnect()

安全配置建议

  1. 不要在pom.xml中硬编码密码

    xml 复制代码
    <configuration>
        <user>${weblogic.username}</user>
        <password>${weblogic.password}</password>
    </configuration>

    然后在settings.xml中配置:

    xml 复制代码
    <servers>
        <server>
            <id>weblogic-server</id>
            <username>weblogic</username>
            <password>welcome1</password>
        </server>
    </servers>
  2. 使用加密密码

    xml 复制代码
    <password>{DES}encrypted_password</password>

多环境部署配置

xml 复制代码
<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

常见问题解决

  1. ClassNotFoundException: weblogic.Deployer

    确保weblogic.jar在类路径中,可以通过设置weblogic.home属性指定WebLogic安装目录。

  2. 认证失败

    检查用户名/密码是否正确,确保用户有部署权限。

  3. 连接超时

    检查WebLogic服务器是否运行,网络是否通畅,防火墙设置。

  4. 版本不兼容

    确保插件版本与WebLogic服务器版本匹配。

以上方法可以根据项目需求选择使用,官方WebLogic Maven插件通常是最简单直接的选择。

相关推荐
Query*8 小时前
Java 设计模式——适配器模式进阶:原理深挖、框架应用与实战扩展
java·设计模式·适配器模式
Sirens.9 小时前
Java核心概念:抽象类、接口、Object类深度剖析
java·开发语言·github
Meteors.9 小时前
23种设计模式——中介者模式 (Mediator Pattern)详解
java·设计模式·中介者模式
望获linux9 小时前
【实时Linux实战系列】使用 u-trace 或 a-trace 进行用户态应用剖析
java·linux·前端·网络·数据库·elasticsearch·操作系统
焰火19999 小时前
[Java]基于Spring的轻量级定时任务动态管理框架
java·后端
Seven979 小时前
Springboot 常见面试题汇总
java·spring boot
程序员阿鹏9 小时前
49.字母异位词分组
java·开发语言·leetcode
云中隐龙9 小时前
mac使用本地jdk启动elasticsearch解决elasticsearch启动时jdk损坏问题
java·elasticsearch·macos
CodeLongBear9 小时前
苍穹外卖 Day12 实战总结:Apache POI 实现 Excel 报表导出全流程解析
java·excel
爱学习 爱分享9 小时前
mac idea 点击打开项目卡死
java·macos·intellij-idea