informatica:linux:java:maven
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
informatica:linux:java:maven [2013/08/13 11:11] – jose | informatica:linux:java:maven [2017/07/26 11:49] (current) – [Proyecto POST] jose | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Instalación ====== | ====== Instalación ====== | ||
- | Descargamos | + | En debian está en los repositorios. |
+ | |||
+ | Podemos también descargamos | ||
./mvn archetype: | ./mvn archetype: | ||
| | ||
Line 22: | Line 24: | ||
====== Aplicación Hello World ====== | ====== Aplicación Hello World ====== | ||
+ | Codigo fuente: \\ | ||
{{: | {{: | ||
+ | |||
+ | Aplicación sola: \\ | ||
+ | {{: | ||
+ | |||
+ | ====== Proyecto Hello World ====== | ||
+ | Fuente: https:// | ||
+ | |||
+ | group id: org.iwanttobefreak.post | ||
+ | artifact id: java-archive | ||
+ | version: 1.0-SNAPSHOT | ||
+ | Creamos el siguiente pom.xml | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | También podemos añadir la versión de java, me daba algún error porque era la 5 por defecto: | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Ahora creamos el código java en esta ruta: | ||
+ | mkdir -p src/ | ||
+ | |||
+ | |||
+ | El nombre del fichero .java, en este caso Main es la clase que declaramos en el código fuente | ||
+ | src/ | ||
+ | < | ||
+ | package org.iwanttobefreak.post; | ||
+ | public class Main { | ||
+ | public static void main(String[] args) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Compilamos el paquete: | ||
+ | mvn package | ||
+ | Nos crea el jar en el directorio target. | ||
+ | |||
+ | Lo ejecutamos: | ||
+ | java -cp ./ | ||
+ | Hello world | ||
+ | |||
+ | ====== Proyecto WGET ====== | ||
+ | < | ||
+ | package org.iwanttobefreak.post; | ||
+ | |||
+ | import java.io.BufferedReader; | ||
+ | import java.io.InputStreamReader; | ||
+ | import java.net.URL; | ||
+ | |||
+ | public class Main { | ||
+ | public static void main(String[] args) { | ||
+ | try { | ||
+ | URL url = new URL(" | ||
+ | BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); | ||
+ | String strTemp = ""; | ||
+ | while (null != (strTemp = br.readLine())) { | ||
+ | System.out.println(strTemp); | ||
+ | } | ||
+ | } catch (Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ====== Proyecto POST ====== | ||
+ | |||
+ | < | ||
+ | package com.iwanttobefreak.post; | ||
+ | |||
+ | import java.io.BufferedReader; | ||
+ | import java.io.DataOutputStream; | ||
+ | import java.io.InputStreamReader; | ||
+ | import java.net.HttpURLConnection; | ||
+ | import java.net.URL; | ||
+ | |||
+ | import javax.net.ssl.HttpsURLConnection; | ||
+ | |||
+ | public class HttpURLConnectionExample { | ||
+ | |||
+ | private final String USER_AGENT = " | ||
+ | |||
+ | public static void main(String[] args) throws Exception { | ||
+ | |||
+ | HttpURLConnectionExample http = new HttpURLConnectionExample(); | ||
+ | |||
+ | System.out.println(" | ||
+ | http.sendPost(); | ||
+ | |||
+ | } | ||
+ | |||
+ | // HTTP POST request | ||
+ | private void sendPost() throws Exception { | ||
+ | |||
+ | String url = " | ||
+ | URL obj = new URL(url); | ||
+ | HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); | ||
+ | |||
+ | //add reuqest header | ||
+ | con.setRequestMethod(" | ||
+ | con.setRequestProperty(" | ||
+ | con.setRequestProperty(" | ||
+ | con.setRequestProperty(" | ||
+ | |||
+ | //Petición JSON si tiene comillas, las trampeamos con \ | ||
+ | String urlParameters = " | ||
+ | |||
+ | // Send post request | ||
+ | con.setDoOutput(true); | ||
+ | DataOutputStream wr = new DataOutputStream(con.getOutputStream()); | ||
+ | wr.writeBytes(urlParameters); | ||
+ | wr.flush(); | ||
+ | wr.close(); | ||
+ | |||
+ | int responseCode = con.getResponseCode(); | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | |||
+ | BufferedReader in = new BufferedReader( | ||
+ | new InputStreamReader(con.getInputStream())); | ||
+ | String inputLine; | ||
+ | StringBuffer response = new StringBuffer(); | ||
+ | |||
+ | while ((inputLine = in.readLine()) != null) { | ||
+ | response.append(inputLine); | ||
+ | } | ||
+ | in.close(); | ||
+ | |||
+ | //print result | ||
+ | System.out.println(response.toString()); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====== Proyecto Out Of Memory OOM ====== | ||
+ | < | ||
+ | public class TestOOM | ||
+ | { | ||
+ | public static void main(String ar[]) | ||
+ | { | ||
+ | String test[]=new String[Integer.MAX_VALUE]; | ||
+ | for(int i=0; | ||
+ | { | ||
+ | test[i]=" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
====== Monitorizar con javamelody ====== | ====== Monitorizar con javamelody ====== | ||
http:// | http:// | ||
- | Hay que descargar los icheros | + | Hay que descargar los ficheros |
http:// | http:// | ||
javamelody-x.x.x.jar | javamelody-x.x.x.jar | ||
Line 37: | Line 202: | ||
| | ||
Editar el fichero web.xml de src/ | Editar el fichero web.xml de src/ | ||
- | Añadir las líneas: | + | Añadir las líneas |
< | < | ||
< | < | ||
Line 51: | Line 216: | ||
</ | </ | ||
</ | </ | ||
+ | |||
+ | Volvemos a compilar app desde el directorio de mi aplicación: | ||
+ | mvn package | ||
+ | Despliego el war |
informatica/linux/java/maven.1376392260.txt.gz · Last modified: 2015/04/13 20:19 (external edit)