Actualización a 27/12/2017
Tenía un proyecto eclipse normal sin web, al que modifiqué unas tonterias de una clase ejecutable. Cuando generé el jar ejecutable, éste corría bien en mi ordenador pero había otros que no corría. dándole vueltas al asunto averigüé que Eclipse Mars y Java 1.8 no eran completatamente compatibles cuando se genera un jar ejecutable. La solución ha sido copiar el proyecto a un Eclipse Oxygen y generar el jar. Y ya va a pesar que se tienen que ejecutar en un entorno
=====================================================================
Mira que me llevo todo el día dándole vueltas al asunto. Quiero hacer un programa que se ejecute cada cierto tiempo para realizar actualizaciones periódicas. Para ello dispongo de un servidor virtual que va a realizar dichas tareas.
Hacer un jar ejecutable es fácil basta en Eclipse hacer File-Export-Java-Runnable JAR File (se selecciona Package Required libraries into generatd JAR). Pero, si estamos trabajando con Maven, y utilizamos la carpeta src/main/resources para guadar propiedades, persitence.xml etc... la cosa cambia.
La solución más acertada que he encontrado ha sido la de Crunchify . Por desgracia o por suerte las dependencias (jars) que se definen en el pom.xml se guardan en la carpeta lib.
Veamos primeramente el fichero pom.xml
CODIGO DE JAVA 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>WSRegJPAFac01</groupId> <artifactId>WSRegJPAFac01</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>WSRegJPAFac01</name> <description>WS resgistre factures Aytos</description> <!-- New repository for JAX implementation not included in jdk 9 --> <!-- See https://stackoverflow.com/questions/15429142/add-maven-repositories-for-a-project-in-eclipse--> <!-- http://openjdk.java.net/jeps/320 --> <!-- repositories> <repository> <id>maven-central-repo1</id> <name>Central repository</name> <url>http://repo1.maven.org/maven2</url> </repository> </repositories--> <properties> <cxf.version>3.2.1</cxf.version> <jax.version>2.3.0</jax.version> <failOnMissingWebXml>false</failOnMissingWebXml> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- Java version--> <java.version>9</java.version> </properties> <dependencies> <!-- WS DEPENDENCIES --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <!-- Jetty is needed if you're are not using the CXFServlet --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> <scope>provided</scope> </dependency> <!-- END WS DEPENDENCIES --> <!-- JPA & SQL SERVER DEPENDENCIES--> <!-- JPA & Postgres --> <!-- Javassist required by Hibernate in some computers --> <!-- https://mvnrepository.com/artifact/org.javassist/javassist --> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.22.0-GA</version> </dependency> <!-- JPA 2.1 Provider --> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.12.Final</version> </dependency> <!-- MS-SQL Server JDBC driver de microsoft = KK--> <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc --> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>6.2.2.jre8</version> <scope>test</scope> </dependency> <!-- MS-SQL Server JDBC JTDS bo de 2013--> <!-- https://mvnrepository.com/artifact/net.sourceforge.jtds/jtds --> <dependency> <groupId>net.sourceforge.jtds</groupId> <artifactId>jtds</artifactId> <version>1.3.1</version> </dependency> <!-- Apache commons string utils .. --> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency> <!--BEGIN Java 9 references to JEE not included in JDK9--> <!-- see http://openjdk.java.net/jeps/320--> <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api --> <!--dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>${jax.version}</version> </dependency--> <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl --> <!-- dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>${jax.version}</version> </dependency--> <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core --> <!-- dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>${jax.version}</version> </dependency--> <!-- https://mvnrepository.com/artifact/javax.activation/javax.activation-api --> <!-- dependency> <groupId>javax.activation</groupId> <artifactId>javax.activation-api</artifactId> <version>1.2.0</version> </dependency--> <dependency> <groupId>com.sun.activation</groupId> <artifactId>javax.activation</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-ri</artifactId> <version>${jax.version}</version> <type>pom</type> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-ri</artifactId> <version>${jax.version}</version> <type>pom</type> </dependency> <dependency> <groupId>javax.transaction</groupId> <artifactId>javax.transaction-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.2</version> </dependency> <!-- dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.1</version> </dependency--> <!-- End of Dependencies required for JAVA 9!!!! --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- END JPA & SQL SERVER DEPENDENCIES --> </dependencies> <!-- Esta si que va bien http://crunchify.com/how-to-create-build-java-project-including-all-dependencies-using-maven-maven-resources-maven-dependency-maven-jar-plugin-tutorial/ --> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> <!-- fork compilation and use the specified executable --> <!-- <fork>true</fork> <executable>javac9</executable> --> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <executions> <execution> <id>copy-resources</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/MiCarpeta</outputDirectory> <resources> <resource> <directory>resources</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.0.2</version> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/MiCarpeta/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>ximodante.aytos.operation.AytosOperationUtil</mainClass> </manifest> <manifestEntries> <Class-Path>.</Class-Path> </manifestEntries> </archive> <finalName>MiCarpeta/MiJar</finalName> </configuration> </plugin> </plugins> </build> </project> |
CODIGO DE JAVA 1.8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>WSRegJPAFac</groupId>
<artifactId>WSRegJPAFac</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>WSRegJPAFac</name>
<packaging>jar</packaging>
<description>Registre Factures Teralco des de Aytos FAC</description>
<properties>
<cxf.version>3.2.0</cxf.version>
<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- WS DEPENDENCIES -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
<!-- END WS DEPENDENCIES -->
<!-- JPA & SQL SERVER DEPENDENCIES-->
<!-- JPA 2.1 Provider -->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
<!-- MS-SQL Server JDBC driver de microsoft = KK-->
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.2.2.jre8</version>
<scope>test</scope>
</dependency>
<!-- MS-SQL Server JDBC JTDS bo de 2013-->
<!-- https://mvnrepository.com/artifact/net.sourceforge.jtds/jtds -->
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- END JPA & SQL SERVER DEPENDENCIES -->
</dependencies>
<!-- Esta si que va bien http://crunchify.com/how-to-create-build-java-project-including-all-dependencies-using-maven-maven-resources-maven-dependency-maven-jar-plugin-tutorial/
-->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/MiCarpeta</outputDirectory>
<resources>
<resource>
<directory>resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/MiCarpeta/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>ximodante.aytos.operation.AytosOperationUtil</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<finalName>MiCarpeta/MiJar</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>
|
Tenemos que centrarnos en la última parte (que tiene fondo azul claro)y sobre todo en los parámetros que están en rojo. Esta parte comprende los plugins necerarios para generar el jar. Siendo:
jar: Es el formato a generar (línea 9)
MiCarpeta: Es la carpeta que cuelga de target donde se generará el jar. (linea 91)
ximodante.aytos.operation.AytosOperationUtil es la clase que tiene el main a ejecutar (linea 170)
MiCarpeta/lib: es donde se guardarán los diferentes jars de las librerías referenciadas. (linea 154)
MiJar: Es el nombre propuesto al fichero jar a generar (MiJar.jar) (linea 178)
Destacar la dependencia javassist que en algunas máquinas la pide Hibernate y en otras no! Por tanto es importante indicarla para no tener sorpresas.
Para obtener el jar hacemos click derecho sobre el proyecto- Run AS- 4 Maven Build y seleccionamos clean install en el apartado goals como se indica en la imagen.
Happy coding!
NOTA: Con java 9 tenemos algunas pequeñas diferencias y además se queja de Lombok:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by lombok.javac.apt.LombokProcessor to field com.sun.tools.javac.processing.JavacProcessingEnvironment.discoveredProcs
WARNING: Please consider reporting this to the maintainers of lombok.javac.apt.LombokProcessor
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[
No hay comentarios :
Publicar un comentario