将tomcat服务器凭据添加到项目的pom而不是settings.xml

joh*_*ims 4 tomcat maven

我希望能够在我的tomcat本地开发安装上构建和部署我的java webapp,而无需在每台开发计算机上将tomcat用户的凭据添加到maven的settings.xml.我发现的每篇文章都需要修改settings.xml.

我已经尝试将标记添加到我的POM文件中,因为当我运行"maven compile"时出现"格式错误的POM"错误,似乎不允许这样做.

<project...>
    ...
    <servers>
        <server>
            <id>localhost</id>
            <username>admin</username>
            <password>password</password>
        </server>
    </servers>
    <dependencies>
    ...
    </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

小智 6

你可以试试这个:

<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <url>${server-url}</url> <path>${deploy-path}</path> <username>${deploy-un}</username> <password>${deploy-pw}</password> </configuration> </plugin>

如果项目中有许多模块,请<profile>在每个模块中使用将每个模块部署到不同的模块URLs.例如:

在模块A中:

<profile> <id>server1</id> <properties> <!-- Replace with URL and authentication if needed --> <server-url>http://localhost:8080/manager/text</server-url> <deploy-path>/moduleA</deploy-path> <deploy-un>tomcatscript</deploy-un> <deploy-pw>p@ssw0rd</deploy-pw> </properties> </profile>

在模块B中:

<profile> <id>server1</id> <properties> <!-- Replace with URL and authentication if needed --> <server-url>http://localhost:8080/manager/text</server-url> <deploy-path>/moduleB</deploy-path> <deploy-un>tomcatscript</deploy-un> <deploy-pw>p@ssw0rd</deploy-pw> </properties> </profile>

请勿忘记将此添加到您的tomcat/conf/tomcat-users.xml:

<role rolename="manager-script"/> <role rolename="manager-jmx"/> <user username="tomcatscript" password="p@ssw0rd" roles="manager-script, manager-jmx"/>

然后在终端,使用这个: mvn tomcat7:[re]deploy -Pserver1

moduleA将部署到http://localhost:8080/moduleA,

moduleB 将被部署到 http://localhost:8080/moduleB

希望这可以帮助!