使用 Intellij 在远程服务器上部署/调试 java 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42380496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:35:02  来源:igfitidea点击:

deploy/debug java code on a remote server using Intellij

javadebuggingintellij-idearemote-accessremote-debugging

提问by HimanAB

I want to run my java code on a remote server for faster speed (The server is very powerful). What I want is to connect my Intellij to that remote server and run my code. But I want to still use the IntelliJ on my local machine (i.e. my laptop).

我想在远程服务器上运行我的 java 代码以获得更快的速度(服务器非常强大)。我想要的是将我的 Intellij 连接到该远程服务器并运行我的代码。但我仍然想在我的本地机器(即我的笔记本电脑)上使用 IntelliJ。

I found a config section in IntelliJ which is in Default Setting->Build-executation-deployment-> Deploymentand there I can set the address of my remote server and username and password. But I don't know what to do next.

我在 IntelliJ 中找到了一个配置部分,在 Default Setting->Build-executation-deployment-> Deployment那里我可以设置远程服务器的地址以及用户名和密码。但我不知道接下来要做什么。

回答by CrazyCoder

There is a step by step deployment guidefor PhpStorm, but for IntelliJ IDEA it would be almost the same.

PhpStorm有一个分步部署指南,但对于 IntelliJ IDEA,它几乎是相同的。

Here is the example configuration for deploying a .jarfile from artifactsubdirectory to the remote server via SFTP into /home/serge/artifactdirectory:

以下是通过 SFTP将.jar文件从artifact子目录部署到远程服务器到/home/serge/artifact目录的示例配置:

connection

联系

mappings

映射

I'd configure the artifact to produce the executable jar. Then adjust the deployment configuration to deploy the jar to the remote server. Or you can use Maven/Gradle to do the same.

我会配置工件以生成可执行的 jar。然后调整部署配置,将jar部署到远程服务器。或者你可以使用 Maven/Gradle 来做同样的事情。

Next, you can configure Remote SSH external toolto run the jar on the server (via java -jar jarname.jar:

接下来,您可以配置远程 SSH 外部工具以在服务器上运行 jar(通过java -jar jarname.jar

remote ssh tool

远程ssh工具

Running on the remote server via Tools| External Tools| hello:

通过Tools|在远程服务器上运行 External Tools| hello

running

跑步

To automate the process enable Include in project buildfor the artifact, enable Automatic uploadin Tools| Deploymentand enable uploading of external changesin Tools| Deployment| Options.

要自动化该过程,请在工件的项目构建中启用包含Automatic uploadTools| 中启用。Deployment并启用在| 中上传外部更改 | .ToolsDeploymentOptions

To debug the code use Remote Debug configuration. Copy the JVM options needed for debug and adjust the options in your remote SSH external tool so that the app is started in debug mode and can accept connections (make sure firewall rules are adjusted to permit the connections on the specified port).

要调试代码,请使用远程调试配置。复制调试所需的 JVM 选项并调整远程 SSH 外部工具中的选项,以便应用程序以调试模式启动并可以接受连接(确保调整防火墙规则以允许指定端口上的连接)。

回答by arghtype

If you need to debug your web application running on remote server you can do it this way:

如果您需要调试在远程服务器上运行的 Web 应用程序,您可以这样做:

Deploy your code to remote server.There are several ways to do it:

将您的代码部署到远程服务器。有几种方法可以做到:

  • By intergration IDEA with you application server. Go into Settings > Build, Execution, Deployments > Application Serversand add your application server there. You could later use it as deployment target. See documentation.
  • By integration via you building tool, for example, maven have plugins for integration with many app servers. This works well when your build process is complicated.
  • Manually - simple copy build artifacts into target app server machine and deploy manually.
  • 通过将 IDEA 与您的应用程序服务器集成。进入Settings > Build, Execution, Deployments > Application Servers并在那里添加您的应用程序服务器。您可以稍后将其用作部署目标。请参阅文档
  • 例如,通过您的构建工具集成,maven 具有用于与许多应用服务器集成的插件。当您的构建过程很复杂时,这很有效。
  • 手动 - 简单地将构建工件复制到目标应用服务器机器并手动部署。

Connect to server in debug mode.To do this you need to create separate Run/ Debug configuration in IDEA. If you have Enterprise Edition you could choose configuration template for your server (e.q. Tomcat Server) and choose server from your Application Serverslist. In Community Edition you have to use default Remoteconfiguration instead.

在调试模式下连接到服务器。为此,您需要在 IDEA 中创建单独的运行/调试配置。如果您有企业版,您可以为您的服务器选择配置模板(如Tomcat 服务器)并从您的应用程序服务器列表中选择服务器。在社区版中,您必须改用默认的远程配置。

When setup is done, your workflow should be the following:

设置完成后,您的工作流程应如下所示:

  1. Make changes into code;
  2. Redeploy it into server (restart it if necessary);
  3. Run your debug configuration;
  4. Access your application on server (via browser for example) to trigger required code for execution;
  5. Debug
  1. 对代码进行更改;
  2. 将其重新部署到服务器中(必要时重新启动);
  3. 运行您的调试配置;
  4. 在服务器上访问您的应用程序(例如通过浏览器)以触发执行所需的代码;
  5. 调试