Deployment Angular: Unterschied zwischen den Versionen

Aus Tutorials
Zur Navigation springen Zur Suche springen
 
(33 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 2: Zeile 2:




== Multimodule Maven Project ==
== Als Multimodule Maven Project ==


=== Parent POM ===
Siehe dazu [[Multimodule Maven Project]]


In der Konsole folgenden Befehl aufrufen:
=== Links ===
 
[https://www.devglan.com/spring-boot/spring-boot-angular-deployment https://www.devglan.com/spring-boot/spring-boot-angular-deployment]
 
== Als Maven Dependency ==
 
=== Dependency erstellen ===


<pre>
<pre>
mvn archetype:generate \
cd dist/<projekt folder>/
-Dfilter=pom-root \
ng build --prod
-DarchetypeVersion=RELEASE \
zip -r angular-client.zip *
-DgroupId=at.or.kirner \
 
-DartifactId=angular-rest-test
# Kontrolle der Zip-Datei
unzip -l angular-client.zip
 
mvn install:install-file \
    -Dfile=angular-client.zip \
    -DgroupId=at.or.kirner \
    -DartifactId=<project name>-client \
    -Dversion=0.1 \
    -Dpackaging=zip \
    -DgeneratePom=true
</pre>
</pre>


<code>-DarchetypeVersion=RELEASE</code> verwendet automatisch die letzte Version
==== Links ====
 
[https://stackoverflow.com/questions/4955635/how-to-add-local-jar-files-to-a-maven-project https://stackoverflow.com/questions/4955635/how-to-add-local-jar-files-to-a-maven-project]
 
=== Dependency in WAR entpacken ===
 
<pre>
...
 
<properties>
  ...
  <client-version>${project.version}</client-version>
  ...
</properties>


<code>-DgroupId=<groupId></code> und <code>-DartifactId=<artifactId></code> müssen entsprechend angepasst werden.
...


<plugin>
  <!-- copy the latest angular client from the repository into the war file -->
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>${dependency-plugin-version}</version>
  <executions>
    <execution>
      <id>unpack-web-client</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>unpack</goal>
      </goals>


Als Ergebnis wurde folgende POM-Datei generiert:
      <configuration>
        <artifactItems>
          <artifactItem>
            groupId>at.or.kirner</groupId>
            <artifactId><project name>-client</artifactId>
            <version>${project.version}</version>
            <type>zip</type>
            <overWrite>true</overWrite>
            <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/angular-client</outputDirectory>
          </artifactItem>
        </artifactItems>
      </configuration>
    </execution>
  </executions>
</plugin>


<pre>
...
<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>at.or.kirner</groupId>
  <artifactId>angular-rest-test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>angular-rest-test</name>
</project>
</pre>
</pre>


=== Sub-Module ===
==== Links ====
 
[https://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-artifacts.html https://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-artifacts.html]
 
== webpack ==
 
{{note|Aktuell nicht verwendet}}
 
[[webpack (Angular)|webpack]]
 
== base-href ==
 
Da sich durch das Integrieren des Angular-Front-End in der WAR-Datei der Pfad ändert (z.B. 'http://10.0.0.100:8080/video-archive/archive-client') muss auch der Basispfad in Angular geändert werden.
 
 
Dazu im <code><head></code>-Abschnitt vom ''index.html'' <code><base href="/"></code> folgendermaßen anpassen:


<pre>
<pre>
cd angular-rest-test
<script>
  if (window.location.port == 4200) {
    document.write('<base href="/">');
  } else {
    document.write('<base href="/video-archive/archive-client/">');
  }
  window['base-href'] = window.location.pathname;
</script>
</pre>
</pre>
Und im Basis-Modul (z.B. 'app.module.ts') gehört ''APP_BASE_HREF'' angepasst:


<pre>
<pre>
mvn archetype:generate \
import { APP_BASE_HREF } from '@angular/common';
-DarchetypeGroupId=org.apache.maven.archetypes \
...
-DarchetypeArtifactId=maven-archetype-quickstart \
@NgModule({
-DarchetypeVersion=RELEASE \
...
-DgroupId=at.or.kirner \
  providers: [
-DartifactId=service
    { provide: APP_BASE_HREF, useValue: window['base-href'] }
  ]
})
...
</pre>
</pre>
=== Links ===
[https://medium.com/@anandshende1994/setting-base-href-dynamically-in-angular-6-b7fe824848cf https://medium.com/@anandshende1994/setting-base-href-dynamically-in-angular-6-b7fe824848cf]
== Reloading ==
In <code>src/main/webapp/WEB-INF/web.xml</code> folgenden Eintrag für Error-Page hinzufügen:


<pre>
<pre>
mvn archetype:generate -DgroupId=at.or.kirner  -DartifactId=service
<error-page>
mvn archetype:generate -DgroupId=at.or.kirner  -DartifactId=webapp
    <error-code>404</error-code>
    <location>/index.html</location>
</error-page>
</pre>
</pre>


=== Links ===
=== Links ===


[https://www.baeldung.com/maven-multi-module https://www.baeldung.com/maven-multi-module]
[https://stackoverflow.com/questions/47366792/404-error-on-refresh-for-angularv4-deployed-on-tomcat-server https://stackoverflow.com/questions/47366792/404-error-on-refresh-for-angularv4-deployed-on-tomcat-server]
 
== Apache HTTP ==


[https://stackoverflow.com/questions/6328778/how-to-create-an-empty-multi-module-maven-project https://stackoverflow.com/questions/6328778/how-to-create-an-empty-multi-module-maven-project]
<pre>
<VirtualHost *:80>
        ServerName video.kirner.or.at


== webpack ==
        RewriteEngine On


{{note|Aktuell nicht verwendet}}
        RewriteOptions          InheritDownBefore


[[webpack (Angular)|webpack]]


== Als Sub-Module ==
        # If an existing asset or directory is requested go to it as it is
#        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
#        RewriteCond http://10.2.0.100:8080/video-archive/archive-client/%{REQUEST_URI} -f [OR]
#        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
#        RewriteCond http://10.2.0.100:8080/video-archive/archive-client/%{REQUEST_URI} -d
#        RewriteRule / %{REQUEST_SCHEME}://10.2.0.99:8080/video-archive/archive-client/index.html [L]


=== Links ===
        RewriteRule            "/(.*)$"  "%{REQUEST_SCHEME}://10.2.0.100:8080/video-archive/archive-client/$1" [P]


[https://www.devglan.com/spring-boot/spring-boot-angular-deployment https://www.devglan.com/spring-boot/spring-boot-angular-deployment]
        ErrorLog ${APACHE_LOG_DIR}/video.kirner.or.at-error.log
        CustomLog ${APACHE_LOG_DIR}/video.kirner.or.at-access.log combined
</VirtualHost>
</pre>


== base-href ==
[https://httpd.apache.org/docs/trunk/rewrite/flags.html#flag_p https://httpd.apache.org/docs/trunk/rewrite/flags.html#flag_p]


=== Links ===
=== Links ===


[https://medium.com/@anandshende1994/setting-base-href-dynamically-in-angular-6-b7fe824848cf https://medium.com/@anandshende1994/setting-base-href-dynamically-in-angular-6-b7fe824848cf]
[https://angular.io/guide/deployment https://angular.io/guide/deployment]
 
[https://www.netnea.com/cms/apache-tutorial-9-reverse-proxy-einrichten/ https://www.netnea.com/cms/apache-tutorial-9-reverse-proxy-einrichten/]
 
[https://httpd.apache.org/docs/trunk/rewrite/ https://httpd.apache.org/docs/trunk/rewrite/]




Zurück zu [[Angular#Deployment|Angular]]
Zurück zu [[Angular#Deployment|Angular]]

Aktuelle Version vom 21. Juli 2020, 19:23 Uhr

Noch in Bearbeitung


Als Multimodule Maven Project

Siehe dazu Multimodule Maven Project

Links

https://www.devglan.com/spring-boot/spring-boot-angular-deployment

Als Maven Dependency

Dependency erstellen

cd dist/<projekt folder>/
ng build --prod
zip -r angular-client.zip *

# Kontrolle der Zip-Datei
unzip -l angular-client.zip 

mvn install:install-file \
    -Dfile=angular-client.zip \
    -DgroupId=at.or.kirner \
    -DartifactId=<project name>-client \
    -Dversion=0.1 \
    -Dpackaging=zip \
    -DgeneratePom=true

Links

https://stackoverflow.com/questions/4955635/how-to-add-local-jar-files-to-a-maven-project

Dependency in WAR entpacken

...

<properties>
  ...
  <client-version>${project.version}</client-version>
  ...
</properties>

...

<plugin>
  <!-- copy the latest angular client from the repository into the war file -->
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>${dependency-plugin-version}</version>
  <executions>
    <execution>
      <id>unpack-web-client</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>unpack</goal>
      </goals>

      <configuration>
        <artifactItems>
          <artifactItem>
            groupId>at.or.kirner</groupId>
            <artifactId><project name>-client</artifactId>
            <version>${project.version}</version>
            <type>zip</type>
            <overWrite>true</overWrite>
            <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/angular-client</outputDirectory>
          </artifactItem>
        </artifactItems>
      </configuration>
    </execution>
  </executions>
</plugin>

...

Links

https://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-artifacts.html

webpack

Aktuell nicht verwendet

webpack

base-href

Da sich durch das Integrieren des Angular-Front-End in der WAR-Datei der Pfad ändert (z.B. 'http://10.0.0.100:8080/video-archive/archive-client') muss auch der Basispfad in Angular geändert werden.


Dazu im <head>-Abschnitt vom index.html <base href="/"> folgendermaßen anpassen:

<script>
  if (window.location.port == 4200) {
    document.write('<base href="/">');
  } else {
    document.write('<base href="/video-archive/archive-client/">');
  }
  window['base-href'] = window.location.pathname;
</script>

Und im Basis-Modul (z.B. 'app.module.ts') gehört APP_BASE_HREF angepasst:

import { APP_BASE_HREF } from '@angular/common';
...
@NgModule({
...
  providers: [
    { provide: APP_BASE_HREF, useValue: window['base-href'] }
  ]
})
...

Links

https://medium.com/@anandshende1994/setting-base-href-dynamically-in-angular-6-b7fe824848cf

Reloading

In src/main/webapp/WEB-INF/web.xml folgenden Eintrag für Error-Page hinzufügen:

<error-page>
    <error-code>404</error-code>
    <location>/index.html</location>
</error-page>

Links

https://stackoverflow.com/questions/47366792/404-error-on-refresh-for-angularv4-deployed-on-tomcat-server

Apache HTTP

<VirtualHost *:80>
        ServerName video.kirner.or.at

        RewriteEngine On

        RewriteOptions          InheritDownBefore


        # If an existing asset or directory is requested go to it as it is
#        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
#        RewriteCond http://10.2.0.100:8080/video-archive/archive-client/%{REQUEST_URI} -f [OR]
#        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
#        RewriteCond http://10.2.0.100:8080/video-archive/archive-client/%{REQUEST_URI} -d
#        RewriteRule / %{REQUEST_SCHEME}://10.2.0.99:8080/video-archive/archive-client/index.html [L]

        RewriteRule             "/(.*)$"   "%{REQUEST_SCHEME}://10.2.0.100:8080/video-archive/archive-client/$1" [P]

        ErrorLog ${APACHE_LOG_DIR}/video.kirner.or.at-error.log
        CustomLog ${APACHE_LOG_DIR}/video.kirner.or.at-access.log combined
</VirtualHost>

https://httpd.apache.org/docs/trunk/rewrite/flags.html#flag_p

Links

https://angular.io/guide/deployment

https://www.netnea.com/cms/apache-tutorial-9-reverse-proxy-einrichten/

https://httpd.apache.org/docs/trunk/rewrite/


Zurück zu Angular