> ## Documentation Index
> Fetch the complete documentation index at: https://blog.mapptech.com.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Guía de configuración de WSL + VS Code + Tomcat 9

> Esta guía te lleva paso a paso por la configuración global única en tu máquina, y luego por los pasos por proyecto para poner **PREDIUM** en funcionamiento dentro de WSL con VS Code y Tomcat 9.0.85.

## Parte A: Configuración global única de WSL

### 1. Habilitar e inicializar WSL 2

```powershell theme={null}
# En PowerShell (Admin):
wsl --install
wsl --set-default-version 2
```

Reinicia si se te solicita. Luego lanza tu distro Linux (p. ej. Ubuntu) desde el menú Inicio y completa la configuración inicial.

### 2. Ocultar globalmente los ADS de Windows

```ini theme={null}
# En WSL, edita /etc/wsl.conf:
[automount]
options = "metadata,streams_interface=none"
```

```powershell theme={null}
# De regreso en PowerShell (Admin):
wsl --shutdown
```

Reinicia la distro: ya no verás los archivos `*:Zone.Identifier`.

### 3. Configurar tu entorno de shell

```bash theme={null}
# Añade al final de ~/.bashrc (o ~/.profile):
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export CATALINA_HOME=/opt/tomcat9

# Recarga:
source ~/.bashrc
```

***

## Parte B: Instalación de herramientas y Tomcat en WSL

### 4. Instalar herramientas básicas

```bash theme={null}
sudo apt update
sudo apt install -y openjdk-8-jdk ant unzip curl
```

### 5. Instalar Tomcat 9.0.85

```bash theme={null}
# Descargar y extraer
cd /tmp
wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.85/bin/apache-tomcat-9.0.85.tar.gz
sudo mkdir -p /opt/tomcat9
sudo tar xzf apache-tomcat-9.0.85.tar.gz \
  -C /opt/tomcat9 --strip-components=1

# Crear usuario/grupo tomcat y ajustar permisos
sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat9 tomcat
sudo chown -R tomcat:tomcat /opt/tomcat9
sudo chmod -R o+rX /opt/tomcat9
sudo chmod +x /opt/tomcat9/bin/*.sh
```

### 6. Verificar Tomcat

```bash theme={null}
sudo -u tomcat $CATALINA_HOME/bin/startup.sh
```

En tu navegador de Windows abre **[http://localhost:8080](http://localhost:8080)**: deberías ver la página de bienvenida de Tomcat.

***

## Parte C: Flujo de trabajo en VS Code y el proyecto

### 7. Instalar y configurar VS Code Remote-WSL

1. Instala **Visual Studio Code** en Windows.
2. En VS Code → Extensiones (Ctrl+Shift+X) instala **Remote – WSL**.
3. Haz clic en el icono verde `><` (abajo a la izquierda) → **Remote-WSL: New Window**.
4. En esa ventana conectada a WSL, instala:
   * **Extension Pack for Java** (`vscjava.vscode-java-pack`)
   * **Tomcat for Java** (`adashen.vscode-tomcat`)

### 8. Copiar el proyecto PREDIUM a WSL

* Desde el Explorador de Windows, copia tu carpeta `PREDIUM` a\
  `\\wsl$\Ubuntu\home\santiago\projects\PREDIUM`
* O en WSL:
  ```bash theme={null}
  mkdir -p ~/projects
  cp -r /mnt/c/Ruta/Al/Proyecto/PREDIUM ~/projects/
  ```

### 9. Abrir el proyecto en VS Code

```bash theme={null}
cd ~/projects/PREDIUM
code .
```

:::note
En la primera ejecución verás **“Installing VS Code Server for x64…”** en tu terminal de WSL. **Por favor espera** a que termine antes de interactuar con el editor —de lo contrario podrías terminar editando archivos en Windows por error.
:::

### 10. Registrar Tomcat en VS Code

* En la ventana conectada a WSL → vista **Tomcat Servers** (icono de elefante o Ctrl+Shift+P → `Tomcat: Show Servers View`).
* Haz clic en **➕ Add Tomcat Server**, selecciona `/opt/tomcat9`.
* Aparecerá **Local Tomcat 9.0.85** en la lista.

### 11. Compilar y empaquetar el WAR

```bash theme={null}
cd ~/projects/PREDIUM
ant clean dist
```

Al finalizar tendrás `dist/PREDIUM.war`.

### 12. Desplegar y ejecutar en VS Code

* En **Explorer**, haz clic derecho en `dist/PREDIUM.war` → **Run on Tomcat Server** → **Local Tomcat 9.0.85**.
* VS Code desplegará y arrancará tu aplicación.

### 13. Verificar y depurar

* Abre **[http://localhost:8080/PREDIUM/](http://localhost:8080/PREDIUM/)** en el navegador para ver tu app.
* Para depurar: en **Tomcat Servers** haz clic derecho en el servidor → **Debug** → selecciona el mismo WAR; establece puntos de interrupción en VS Code.

### 14. Conectar PostgreSQL en Windows desde WSL (v16, copiar & pegar)

1. **Configurar PostgreSQL en Windows y abrir el firewall**
   ```powershell theme={null}
   $winIp = (Get-NetIPConfiguration | Where-Object { $_.IPv4DefaultGateway -ne $null } | Select-Object -Expand IPv4Address).IPAddress
   $pgRoot = 'C:\Program Files\PostgreSQL\16'
   & "$pgRoot\bin\psql.exe" -U postgres -d postgres -c "ALTER SYSTEM SET listen_addresses='*';"
   $hba = "$pgRoot\data\pg_hba.conf"
   Add-Content $hba ("host all all " + $winIp + "/32 md5")
   Restart-Service postgresql-x64-16
   New-NetFirewallRule -DisplayName 'Postgres (WSL)' -Direction Inbound -Protocol TCP -LocalPort 5432 -Action Allow -Profile Private
   ```

2. **Obtener la IP de Windows dentro de WSL**
   ```bash theme={null}
   export DB_HOST=$(
     powershell.exe -NoProfile -Command \
       "(Get-NetIPConfiguration | Where-Object { \$_.IPv4DefaultGateway -ne \$null }).IPv4Address.IPAddress"
   )
   DB_HOST=${DB_HOST//$'\r'/}
   echo "DB_HOST → $DB_HOST"
   ```

3. **Instalar el cliente y verificar la conexión**
   ```bash theme={null}
   sudo apt update
   sudo apt install -y postgresql-client
   psql -h "$DB_HOST" -U postgres -d predium_vup
   ```

4. **Configurar tu aplicación Spring/Hibernate**\
   Reemplaza `<TU_IP_WINDOWS>` con el valor que obtuviste en el paso 2:
   ```xml theme={null}
   <property name="url" value="jdbc:postgresql://<TU_IP_WINDOWS>:5432/predium_vup"/>
   ```

***

##### Consejos extra

* **Cambiar puerto HTTP**: edita `/opt/tomcat9/conf/server.xml`.
* **Ver logs**: panel Output de VS Code → selecciona **Tomcat Server**.
* **Reiniciar**: clic derecho en el servidor → **Restart**.
