Programing

Maven으로 시작할 때 Tomcat의 대체 포트 (8080이 아님)?

lottogame 2020. 10. 26. 07:36
반응형

Maven으로 시작할 때 Tomcat의 대체 포트 (8080이 아님)?


pom 또는 명령 줄에서 Tomcat에 대한 대체 포트를 쉽게 지정할 수있는 방법이 있습니까? 동일한 컴퓨터에서 여러 프로젝트를 실행하고 싶습니다.


이 실이 오래되었다는 건 알지만 ...

Greg가 제공 한 문서에 대한 링크는 흥미 롭습니다.

port:
The port to run the Tomcat server on.
    Type: int
    Required: No
    Expression: ${maven.tomcat.port}
    Default: 8080

표현식은 maven이 코드에서 값을 얻기 위해 사용하는 것입니다. 구성 파일 또는 명령 줄에서 가져올 수 있습니다.

당신은 실행할 수 있습니다

mvn -Dmaven.tomcat.port=8181 tomcat:run-war

동시에 통합 테스트 단계를 실행하는 여러 개의 작은 서블릿이있을 때 비슷한 문제가 있었는데, 이는 동일한 포트를 사용하도록 구성 되었기 때문에 문제가되었습니다. 그러나 build-helper-maven-plugin : reserve-network-port 목표 덕분에 사용 가능한 임의의 포트 번호를 얻을 수 있습니다. 그럼이 포함 된 URL을 만들 수 있습니다 에 http : // localhost를 : [포트] / [servletname와] 입니다, 로 공급 자바 테스트 클래스.

임의 포트 검색 :

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
    <execution>
        <id>reserve-network-port</id>
        <goals>
            <goal>reserve-network-port</goal>
        </goals>
        <phase>pre-integration-test</phase>
        <configuration>
            <portNames>
                <portName>tomcat.http.port</portName>
            </portNames>
        </configuration>
    </execution>
</executions>

포트로 Tomcat 시작

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<configuration>
    <port>${tomcat.http.port}</port>
    <useTestClasspath>true</useTestClasspath>
</configuration>
....
</plugin>

failsafe 플러그인에서 실행하는 Java 통합 테스트에 URL 제공

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12.3</version>
....
<configuration>
    <systemPropertyVariables>
        <integration-test.url>http://localhost:${tomcat.http.port}/${project.build.finalName}/</integration-test.url>
    </systemPropertyVariables>
</configuration>
</plugin>

자바 코드

public class DownloadAreaIT {
    private static final String URL = System.getProperty("integration-test.url");
}

tomcat-maven-plugin 에 제공된 구문을 사용하여 포트를 직접 지정할 수 있습니다.

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>tomcat-maven-plugin</artifactId>            
  <configuration>          
    <server>tomcat-development-server</server>
    <port>9966</port>
  </configuration>
</plugin>

아래는 나를 위해 일했습니다.

        <properties>
            <maven.tomcat.port>9090</maven.tomcat.port>
        </properties>

        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <port>${maven.tomcat.port}</port>
            </configuration>
        </plugin>

You coud add the port configuration permanently by adding the attribute port to it.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.0</version>
            <configuration>
                <port>9090</port>
            </configuration>
        </plugin>
    </plugins>
</build>

There is best and easy way to change Tomcat (not 8080) when starting with Maven

Just Edit your application.properties(if you have no application.properties file, then create an application.properties file in resources directory of your maven project) file and set below line
server.port=8181 //You can choose your port number.


If you are using the maven tomcat plugin, you can specify a context.xml by adding a plugin configuration block to the pom.xml:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <version>1.0-beta-1</version>
        <configuration>
          <mode>both</mode>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

The default context.xml file used is located at src/main/webapp/META-INF/context.xml.

Set different ports there.


I think best and simplest is this (if your test are properly bind to integration phase):

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>reserve-network-port</id>
            <goals>
                <goal>reserve-network-port</goal>
            </goals>
            <phase>pre-integration-test</phase>
            <configuration>
                <portNames>      
                    <portName>maven.tomcat.port</portName>
                </portNames>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <server>tomcat-development-server</server>
        <port>9090</port>
    </configuration>
</plugin>

After spending about 3 hours on how to change the port in POM.xml, Here is my latest solution.

<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.2</version>
  <configuration>
  <maven.tomcat.port>8081</maven.tomcat.port>
  </configuration>
 </plugin>

Using just port was not working since that is not a property that you can set in <configuration>. We need to understand what is causing the problem. In my case, error was that port 8080 is taken. I changed the port in server.xml to 8081 but maven does not take it from there. We need to specifically tell it in configuration field. This is where <maven.tomcat.port>8081</maven.tomcat.port> comes in rescue. Note: You can chnage the port 8081 to something else you like.


I know it's a very old question which not have answer yet.

And I've got the similar question when I need to convert a old project which use outer tomcat to embedded tomcat use tomcat7-maven-plugin.

And what I need is build an executable jar. But existing answer cannot work for me... whatever i run java -jar project-name.jar after mvn package. it's always running on port 8080 which is not i wanted... Then I search the doc Executable War and what i fixed is just add a param in command

java -jar my_project.jar -httpPort 9091

which in doc:

usage: java -jar [path to your exec war jar] ...

-httpPort http port to use

-httpsPort https port to use ...

hope it useful.

참고URL : https://stackoverflow.com/questions/646649/alternate-port-for-tomcat-not-8080-when-starting-with-maven

반응형